使用Matplotlab画图
1.绘制折线图
#! /usr/bin/env python
#encoding=utf-8
# 用于python2
import sys reload(sys) sys.setdefaultencoding('utf-8') # matplotlib绘制图片汉字不能正常显示问题 from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False import numpy as np import matplotlib.pyplot as plt x = ["test" + str(i) for i in range(0, 20)] y = np.random.rand(20) # 设置图像窗口大小 plt.figure(figsize=(12, 8), dpi=80) plt.plot(x, y) # 数字和字符串一一对应, 数据的长度一样, ratation旋转的度数 plt.xticks(x[::3], x[::3], rotation=90) # labelpad Spacing in points between the label and the x-axis plt.xlabel(u"电影名称", labelpad=10) plt.ylabel(u"上座率", labelpad=10) plt.title(u"上座率变化情况") plt.show()
2.绘制散点图
from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False import numpy as np import matplotlib.pyplot as plt x = ["电影" + str(i) for i in range(0, 20)] y = np.random.randint(20, 200, 20) # 设置图像窗口大小 plt.figure(figsize=(12, 8), dpi=80) plt.scatter(x, y, alpha=0.4, edgecolors='white') # 数字和字符串一一对应, 数据的长度一样, ratation旋转的度数 plt.xticks(x, x, rotation=90) # labelpad Spacing in points between the label and the x-axis plt.xlabel(u"电影名称", labelpad=10) plt.ylabel(u"上座数量", labelpad=10) plt.title(u"上座数量变化情况") plt.show()
3.绘制水平条形图
from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False import numpy as np import matplotlib.pyplot as plt x = ["电影" + str(i) for i in range(0, 20)] y = np.random.randint(20, 200, 20) y = sorted(y, reverse=True) # 设置图像窗口大小 plt.figure(figsize=(12, 8), dpi=80) plt.barh(range(len(x)), y, height=0.5, color='red') plt.yticks(range(len(x)), x, color='black') plt.grid(alpha=0.3) plt.show()
4.绘制条形图
# 解决matplotlib绘制图片汉字不能正常显示问题 from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False import numpy as np import matplotlib.pyplot as plt x = ["电影" + str(i) for i in range(0, 20)] y = np.random.randint(20, 200, 20) y = sorted(y, reverse=True) # 设置图像窗口大小 plt.figure(figsize=(12, 8), dpi=80) plt.bar(range(len(x)), y, width=0.6) # 数字和字符串一一对应, 数据的长度一样, ratation旋转的度数 plt.xticks(range(len(x)), x, rotation=90) # labelpad Spacing in points between the label and the x-axis plt.xlabel(u"电影名称", labelpad=10) plt.ylabel(u"上座数量", labelpad=10) plt.title(u"上座数量变化情况") plt.show()
5.绘制饼图
# matplotlib绘制图片汉字不能正常显示问题 from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False import matplotlib.pyplot as plt import random # 学习时间分配 pro_name = ["C++", "Python", "Java", "Go", "shell"] pro_time = [10, 15, 5, 3, 1] # 画饼 # plt.pie(pro_time, labels=pro_name, autopct="%3.2f%%", colors=["#ea6f5a", "#509839", "#0c8ac5", "#d29922", "#fdf6e3"]) plt.pie(pro_time, labels=pro_name, autopct="%3.2f%%") # 指定标题 plt.title("学习时间分配") # 保证为图形为正圆 plt.axis("equal") plt.legend(loc="best") plt.show()
6.绘制多条折线图
# 解决matplotlib绘制图片汉字不能正常显示问题 from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False import numpy as np import matplotlib.pyplot as plt import random x = [h for h in range(0, 24)] hainan_y = [random.randint(15, 25) for t in range(0, 24)] # 设置画板属性 plt.figure(figsize=(12, 6), dpi = 100) # 往画板绘图 plt.plot(x, hainan_y, label="海南") # 模拟北京一天内温度的变化 # 生成y轴的温度随机值(5, 10) beijing_y = [random.randint(7, 13) for t in range(0, 24)] # 往画板绘图 plt.plot(x, beijing_y, label="北京") # 模拟河北一天内温度的变化 hebei_y = [random.randint(1, 5) for t in range(0, 24)] # 自定义绘制属性: 颜色color="#0c8ac5", linestyle"-"""--""-.":", 线宽linewidth, 透明度alpha plt.plot(x, hebei_y, label="河北") # 生成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.show()
7.绘制多个条形图
# 解决matplotlib绘制图片汉字不能正常显示问题 from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False import numpy as np import matplotlib.pyplot as plt x = ['2019-06-10', '2019-06-11', '2019-06-12', '2019-06-13', '2019-06-14'] y = [[5000, 3000, 2000], [4000, 3000, 2500], [3000, 2400, 3400], [2000, 1500, 3500], [2000, 1000, 1500]] label = ["最好的我们", '一般的我们', '比较差的我们'] plt.figure(figsize=(12, 8), dpi=80) plt.bar([i-0.1 for i in range(len(x))], [i[0] for i in y], width=0.1, label=label[0]) plt.bar([i for i in range(len(x))], [i[1] for i in y], width=0.1, label=label[1]) plt.bar([i+0.1 for i in range(len(x))], [i[2] for i in y], width=0.1, label=label[2]) plt.xlabel("日期", labelpad=10) plt.ylabel("上座数量", labelpad=10) plt.title("每天对应电影的上座数量") plt.legend(loc="best") #设置x轴的刻度 plt.xticks(range(len(x)), x) plt.show()