Matplotlib 是Python 2D绘图领域的基础套件,它让使用者将数据图形化,并提供多样化的输出格式。这里将会以四个小案例探索Matplotlib的常见用法

绘制折线图

import matplotlib.pyplot as plt

import random

# 保证生成的图片在浏览器内显示

%matplotlib inline

# 保证能正常显示中文(Mac)

plt.rcParams['font.family'] = ['Arial Unicode MS']

# 模拟海南一天的温度变化

# 生成x轴的24小时

hainan_x = [h for h in range(0, 24)]

# 生成y轴的温度随机值(15, 25)

hainan_y = [random.randint(15, 25) for t in range(0, 24)]

# 设置画板属性

plt.figure(figsize = (10, 8), dpi = 100)

# 往画板绘图

plt.plot(hainan_x, hainan_y, label="海南")

# 模拟北京一天内温度的变化

# 生成x轴的24小时

beijing_x = [h for h in range(0, 24)]

# 生成y轴的温度随机值(5, 10)

beijing_y = [random.randint(5, 10) for t in range(0, 24)]

# 往画板绘图

plt.plot(beijing_x, beijing_y, label="北京")

# 模拟河北一天内温度的变化

hebei_x = beijing_x

hebei_y = [random.randint(1, 5) for t in range(0, 24)]

# 自定义绘制属性: 颜色color="#0c8ac5", linestyle"-"""--""-.":", 线宽linewidth, 透明度alpha

plt.plot(hebei_x, hebei_y, label="河北",color="#823384", linestyle=":", linewidth=3, alpha=0.3)

# 坐标轴显示设置

# 生成24小时的描述

x_ = [x_ for x_ in range(0, 24)]

x_desc = ["{}时".format(x_desc) for x_desc in x_]

# 设置x轴显示 24小时

plt.xticks(x_, x_desc)

# 生成10至30度的描述

y_ = [y_ for y_ in range(0, 30)][::2]

y_desc = ["{}℃".format(y_desc) for y_desc in y_]

# 设置y轴显示温度描述

plt.yticks(y_, y_desc)

# 指定x y轴的名称

plt.xlabel("时间")

plt.ylabel("温度")

# 指定标题

plt.title("一天内温度的变化")

# 显示图例

plt.legend(loc="best")

# 将数据生成图片, 保存到当前目录下

plt.savefig("./t.png")

# 在浏览器内展示图片

plt.show()

绘制条形图

import matplotlib.pyplot as plt

import random

# 保证生成的图片在浏览器内显示

%matplotlib inline

# 保证能正常显示中文(Mac)

plt.rcParams['font.family'] = ['Arial Unicode MS']

# 条形图绘制名侦探柯南主要角色年龄

role_list = ["柯南", "毛利兰", "灰原哀", "琴酒","贝尔摩德", "伏特加", "赤井秀一", "目暮十三"]

role_age = [7, 17, 7, 34, 32, 30, 27, 46]

# 实际年龄

role_ture_age = [18, 17, 18, 34, 45, 30, 27, 46]

x = [i for i in range(1, len(role_list)+1)]

y = role_age

y2 =role_ture_age

# 设置画板属性

plt.figure(figsize = (15, 8), dpi = 100)

# width以x为基准,向右为正,向左为负(如果多了,就需要为基准x加减响应的数值)

plt.bar(x, y, width= -0.3, label="现实年龄", color="#509839")

plt.bar(x, y2, width = 0.3, label="实际年龄", color="#c03035")

x_ = [i for i in range(0, len(role_list)+1)]

x_desc = ["{}".format(x_desc) for x_desc in role_list]

x_desc.insert(0, "")

y_ = range(0, 50)[::5]

y_desc = ["{}岁".format(y_desc) for y_desc in range(0, 50)][::5]

# x轴的数值和描述

plt.xticks(x_, x_desc)

plt.yticks(y_, y_desc)

plt.xlabel("角色姓名")

plt.ylabel("年龄")

plt.title("名侦探柯南主要角色年龄(部分)")

plt.legend(loc="best")

plt.savefig("./mzt.png")

plt.show()

直方图

import matplotlib.pyplot as plt

import random

# 保证能正常显示中文

plt.rcParams['font.family'] = ['Arial Unicode MS']

# 时长数据

time = [131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117, 86, 95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137, 92,121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, 133, 101,131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]

max_time = max(time)

min_time = min(time)

# 指定分组宽度

width = 5

# 指定分组数量

num_bins = int((max_time - min_time)/2)

# 直方图统计电影时长频数

plt.figure(figsize=(15, 8), dpi=80)

# 绘制直方图

plt.hist(time, num_bins, color="#509839",normed=1)

# 指定显示刻度的个数

x_ = [i for i in range(min_time, max_time+1)]

plt.xticks(x_[::width])

# 显示网格

plt.grid(True, linestyle="--", alpha=0.5)

# 指定标题

plt.title("Top250的IMDB电影时长统计")

plt.savefig("./IMDB.png")

plt.show()

饼图

import matplotlib.pyplot as plt

import random

# 保证能正常显示中文(Mac)

plt.rcParams['font.family'] = ['Arial Unicode MS']

# 学习时间分配

pro_name = ["C++", "Python", "Java", "Go", "Swift"]

pro_time = [10, 15, 5, 3, 1]

# 画饼

plt.pie(pro_time, labels=pro_name, autopct="%3.2f%%", colors=["#ea6f5a", "#509839", "#0c8ac5", "#d29922", "#fdf6e3"])

# 指定标题

plt.title("学习时间分配")

# 保证为图形为正圆

plt.axis("equal")

# 显示图示

plt.legend(loc="best")

plt.savefig("./pro_learn.png")

plt.show()

python画名侦探柯南_机器学习机器学习三剑客之Matplotlab相关推荐

  1. python画名侦探柯南_名侦探柯南资源

    We Develop Strong . Beautiful . Functional 名侦探柯南资源 We Develop Beautiful & Functional Websites, B ...

  2. python画名侦探柯南_【儿童节特辑】风格迁移——让你的照片秒变手绘日漫风

    写在前面 对于像我这样的小朋友来说(不接受反驳),动漫可是童年的记忆了,还记得<黑猫警长>.<葫芦七兄弟>等国产经典(一不小心,好像暴露年龄了),也还记得<千与千寻> ...

  3. 知道经纬度用python画路线图_神级程序员教你用Python如何画一个中国地图!(好好玩)...

    为什么是Python 先来聊聊为什么做数据分析一定要用 Python 或 R 语言.编程语言这么多种, Java , PHP 都很成熟,但是为什么在最近热火的数据分析领域,很多人选择用 Python ...

  4. 利用python画曲线_利用python画出AUC曲线的实例

    以load_breast_cancer数据集为例,模型细节不重要,重点是画AUC的代码.1XS免费资源网 直接上代码:1XS免费资源网 from sklearn.datasets import loa ...

  5. 用python画熊猫_当熊猫不够用熊猫

    用python画熊猫 Python短裤 (Python Shorts) Pandas is one of the best data manipulation libraries in recent ...

  6. python 画云图_【词云图】如何用python的第三方库jieba和wordcloud画词云图

    一直想学一下如何用python画词云图,觉得很好玩,本文就写一下我自己的一些尝试. 1.提前准备 一般准备以下四样就可以啦. 第一,电脑安装python,我装的是3.6. 第二,安装第三方库jieba ...

  7. 怎么用python画花瓣_怎么用python画花朵

    怎么用python画花朵?下面给大家讲解一下具体步骤: 第一步,打开菜单栏,输入idle,打开shell. 第二步,新建一个文件,并命名. 第三步,导入turtle模块,创建一个新窗口用于绘图,再创建 ...

  8. 用python画风车_用Python画小女孩放风筝的示例

    我就废话不多说了,直接上代码吧! # coding:utf-8 2import turtle as t 3import random 4# 画心 5def xin(): 6 def curvemove ...

  9. 用python画熊猫_熊猫read_excel()–用Python读取Excel文件

    用python画熊猫 We can use the pandas module read_excel() function to read the excel file data into a Dat ...

最新文章

  1. 急需降低系统复杂性,我们从 Kafka 迁移到了 Pulsar
  2. 图像处理中的Mask是什么
  3. Angular全套知识讲解,错过必悔!
  4. boost::count相关的测试程序
  5. dataframe的drop無效
  6. 蒙文字体怎么安装_焘哥带你玩转字体(三)字体的安装及显示问题
  7. 真・WPF 按钮拖动和调整大小
  8. linux ssh 插件,玩转VSCode插件之Remote-SSH的使用情况
  9. python 时分秒毫秒_第一篇:Python处理时间日期
  10. viewpager的使用
  11. java笔试之计算n x m的棋盘格子
  12. 悟透delphi 第五章 包
  13. 程序员计算器HEX、EDC、OCT的意思
  14. Review board 和 Git 配合使用 一
  15. epub文件如何打开?
  16. python安装 错误 “User installations are disabled via policy on the machine”
  17. Android 获取经纬度。2018年写
  18. python充电时刻
  19. 原创|我为什么不建议你等公司倒闭后,再找工作!
  20. 京东2015校园招聘技术类笔试题及答案

热门文章

  1. 如何快速掌握 Python 数据采集与网络爬虫技术
  2. 阿里云APP备案操作流程 新手看过来
  3. 88steam@88skins CSGO饰品皮肤开箱DOTA2饰品开箱即开即取网站
  4. 公众号PHP模板修改,PHP 实现发送模板消息(微信公众号版)
  5. 微信小程序实现横向滚动文字
  6. 山西应用科技学院计算机应用在哪个校区,山西应用科技学院有几个校区,哪个校区最好及各校区介绍...
  7. LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
  8. 魔趣吧致力于为中文站长提供助力
  9. CDH kudu Unable to load consensus metadata for tablet
  10. 小明的烦恼——找字符串