一、基础绘图
1、折线图(matplotlib)
具体步骤参考本人之前的文章
from matplotlib import pyplot as plt
x = range(11,31)
y_1 = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
y_2 = [1,0,3,1,2,2,3,3,2,1,2,1,1,1,1,1,1,1,1,1]
#设置图形大小
plt.figure(figsize=(20,8),dpi=80)
#画两条线,并写明哪条线表示什么,设置线条样式
plt.plot(x,y_1,label="得分1",color="coral",linewidth=5)
plt.plot(x,y_2,label="得分2",color="cyan",linestyle='--')
#设置x轴刻度
_xtick_labels = ["{}岁".format(i) for i in x]
plt.xticks(x,_xtick_labels)
#plt.yticks(range(0,9))
#显示中文字体
plt.rcParams['font.sans-serif'] = ['SimHei',]
#绘制网格,alpha设置网格透明度
plt.grid(alpha=0.5,linestyle=':')
#添加图例(在指定位置显示线条对应的含义)
plt.legend(loc="upper left")
plt.show()
2、条形图
from matplotlib import pyplot as plt
a = []
b = []
plt.figure(figsize=(20,8),dpi=80)
#绘制条形图
plt.bar(range(len(a)),b,width=0.3)
#设置字符串到x轴
plt.xticks(range(len(a)),a,rotation=90)
plt.rcParams['font.sans-serif'] = ['SimHei',]
plt.show()
多条条形图:
from matplotlib import pyplot as plt
a = ["猩球崛起3:终极之战","敦刻尔克","蜘蛛侠:英雄归来","战狼2"]
b_16 = [15746,312,4497,319]
b_15 = [12357,156,2045,168]
b_14 = [2358,399,2358,362]
bar_width = 0.2
x_14 = list(range(len(a)))
x_15 = [i+bar_width for i in x_14]
x_16 = [i+bar_width*2 for i in x_14]
plt.figure(figsize=(8,8),dpi=80)
plt.bar(range(len(a)),b_14,width=bar_width,label="9月14日")
plt.bar(x_15,b_15,width=bar_width,label="9月15日")
plt.bar(x_16,b_16,width=bar_width,label="9月16日")
plt.legend()
plt.xticks(x_15,a)
plt.rcParams['font.sans-serif'] = ['SimHei',]
plt.show()
3、散点图
from matplotlib import pyplot as plt
y_3 = [11,17,16,11,12,11,12,6,6,7,8,9,12,15,14,17,18,21,16,17,20,14,15,15,15,19,21,22,22,22,23]
y_10 = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,13,12,13,6]
x_3 = range(1,32)
x_10 = range(51,82)
#设置图形大小
plt.figure(figsize=(20,8),dpi=80)
#散点图和折线图的区别
plt.scatter(x_3,y_3,label="3月份",color="red")
plt.scatter(x_10,y_10,label="10月份")
_x = list(x_3)+list(x_10)
_xtick_labels = ["3月{}日".format(i) for i in x_3]
_xtick_labels += ["10月{}日".format(i-50) for i in x_10]
plt.xticks(_x[::3],_xtick_labels[::3],rotation=90)
#添加图例
plt.legend(loc="upper left")
#添加描述信息
plt.xlabel("时间")
plt.ylabel("温度")
plt.title("标题")
plt.rcParams['font.sans-serif'] = ['SimHei',]
plt.show()
4、饼图
平面基础饼图
import numpy as np
import matplotlib.pyplot as plt
# 数据
size = [34,20,20,20,6]
# 设置中文
plt.rcParams['font.sans-serif'] = ['SimHei',]
# 绘图(标明各扇形含义)
plt.pie(size, labels=["Windows", "MAC", "Linux", "Android", "Other"])
#设置标题
plt.title("手机系统占比分析")
plt.show()
立体饼图
import matplotlib.pyplot as plt
data = [2052380, 11315444, 20435242, 7456627, 3014264, 1972395, 185028]
# 数据标签
labels = ['none', 'primary', 'junior', 'senior', 'specialties', 'bachelor', 'master']
# 各区域颜色
colors = ['red', 'orange', 'yellow', 'green', 'purple', 'blue', 'black']
# 数据计算处理
sizes = [data[0] / Num * 100, data[1] / Num * 100, data[2] / Num * 100, data[3] / Num * 100, data[4] / Num * 100,
data[5] / Num * 100, data[6] / Num * 100]
# 设置突出模块偏移值
expodes = (0, 0, 0.1, 0, 0, 0, 0)
# 设置绘图属性并绘图
plt.pie(sizes, explode=expodes, labels=labels, shadow=True, colors=colors)
## 用于显示为一个长宽相等的饼图
plt.axis('equal')
# 保存并显示
# plt.savefig('picture/step3/fig3.png')
plt.show()