1、图标常用的辅助元素
2、设置坐标轴的标签、刻度范围和刻度标签
3、添加标题和图例
4、显示网格
5、添加参考线和参考区域
6、添加注释文本
1、图标常用的辅助元素
2、设置坐标轴的标签、刻度范围和刻度标签
2.1、设置坐标轴的标签
注意: axex对象使用set_xlabel() 方法可以设置x轴的标签,使用set_ylabel()方法可以设置y轴标签。set_xlabel()、set_ylabel() 方法与xlabel()、ylabel() 函数的参数用法相同
# 设置x、y轴的标签
plt.xlabel('x轴')
plt.ylabel('y轴')
2.2、设置刻度范围
注意:坐标轴的刻度范围取决于数据中的最大值和最小值
2.3、设置刻度标签
# 设置x轴的刻度范围和刻度标签
plt.xlim(x.min()*1.5,x.max()*1.5)
plt.xticks([-np.pi,-np.pi/2,0,np.pi/2,np.pi],[r'$-\pi$',r'$-\pi/2$',r'$0$',r'$\pi/2$',r'$pi$'])
x = np.linspace(-np.pi,np.pi,100)
y = np.sin(x)
plt.figure()
plt.plot(x,y)
plt.xlabel('x轴')
plt.ylabel('y轴')
plt.xlim(x.min()*1.5,x.max()*1.5)
plt.xticks([-np.pi,-np.pi/2,0,np.pi/2,np.pi],[r'$-\pi$',r'$-\pi/2$',r'$0$',r'$\pi/2$',r'$\pi$'])
plt.show()
实例1:2019年内地电影票票房排行榜
def barh():
labels = ["哪吒之魔童降世", "流浪地球", "复仇者联盟4:终局之战",
"疯狂的外星人", "飞驰人生", "烈火英雄", "蜘蛛侠:英雄远征",
"速度与激情:特别行动", "扫毒2天地对决", "大黄蜂", "惊奇队长",
"比悲伤更悲伤的故事", "哥斯拉2:怪兽之王", "阿丽塔:战斗天使",
"银河补习班"]
bar_width = [48.57, 46.18, 42.05, 21.83, 17.03, 16.70, 14.01, 13.84,
12.85, 11.38, 10.25, 9.46, 9.27, 8.88, 8.64]
y_data = range(len(labels))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.barh(y_data, bar_width, height=0.5,color='orange')
ax.title.set_text('电影')
ax.set_xlabel('总票房(亿元)')
ax.set_ylabel('电影名称')
ax.set_yticks(y_data)
ax.set_yticklabels(labels)
plt.show()
3、添加标题和图例
3.1、添加标题
plt.title('标题名称')
或 set_title('标题名称')
3.2、添加图例
# 添加图例
plt.legend(lines,['正弦','余弦'].shadow=True,fancybox=True)
实例2:支付宝月账单报告
def pie():
kinds = ['购物', '人情往来', '餐饮美食', '通信物流',
'生活日用', '交通出行', '休闲娱乐', '其他']
money_scale = [800 / 3000, 100 / 3000, 1000 / 3000, 200 / 3000,
300 / 3000, 200 / 3000, 200 / 3000, 200 / 3000]
dev_position = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
plt.pie(money_scale, autopct='%3.1f%%', shadow=True,
explode=dev_position, startangle=90)
plt.title('支付宝月账单报告')
plt.legend(kinds,loc='upper right', bbox_to_anchor=[1.3,1.1])
plt.show()
4、显示网格
#显示水平网格
plt.grid(b=True,axis='y',linewidth=0.3)
实例3:汽车速度与制动距离的关系
# 添加网格
def scatter():
y = [0.5, 2.0, 4.4, 7.9, 12.3,
17.7, 24.1, 31.5, 39.9, 49.2,
59.5, 70.8, 83.1, 96.4, 110.7,
126.0, 142.2, 159.4, 177.6, 196.8]
y_data = range(len(y))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(y_data, y)
ax.title.set_text('汽车速度与制动距离的关系')
ax.set_xlabel('汽车速度')
ax.set_ylabel('制动距离')
plt.grid(b=True,linewidth=0.3)
plt.show()
5、添加参考线和参考区域
5.1、添加参考线
plt.axhline(x=0,linestyle='--')
plt.axvline(y=0,linestyle='--')
5.2、添加参考区域
#添加参考区域
plt.axvspan(xmin=0.5,xmax=2.0,alpha=0.3)
plt.axhspan(ymin=0.5,ymax=2.0,alpha=0.3)
实例4:全校高二年级各班男女英语成绩评估
def bar():
y = {'男':[98,97,98.5,96,99,98.5],
'女':[97.5,99,97,98,96.5,98.5]}
label_list = ["高二1班", "高二2班", "高二3班", "高二4班", "高二5班", "高二6班"]
x = np.arange(1,len(y['男'])+1)
print(x)
plt.figure()
plt.title('全校高二年级各班男女英语成绩评估')
plt.bar(x,height=y['男'],linewidth=2,width=0.3,alpha=1)
plt.bar(x+0.35,height=y['女'],linewidth=2,width=0.3,alpha=1)
plt.xlabel('班级')
plt.ylabel('成绩')
plt.legend(['男','女'],loc='upper right')
plt.xticks((x+x+0.35)/2,label_list)
plt.axhline(y=np.mean(y['女']+y['男']),linestyle='--')
plt.show()
6、添加注释文本
6.1、添加指向型注释文本
#添加指向型注释文本
plt.annotate('最小值',xy=(-np.pi/2,-1.0),xytext=(-(np.pi/2),-0.5),arrowprops=dict(arrowsytle="->"))
6.2、添加无指向型注释文本
#添加无指向型注释文本
plt.text(3.19,0.10,"y=sin(x)",bbox=dict(alpha=0.2))
实例5:2013~2019年阿里巴巴淘宝和天猫平台的GMV(在柱形的顶部标注柱形代表的具体数值)
def barr():
y = [10770, 16780, 24440, 30920, 37670, 48200, 57270]
label_list = ["FY2013", "FY2014", "FY2015", "FY2016", "FY2017", "FY2018", "FY2019"]
x = np.arange(1,len(y)+1)
plt.figure()
plt.title('2013~2019年阿里巴巴淘宝和天猫平台的GMV')
plt.bar(x,height=y,tick_label=label_list,linewidth=2,width=0.5,alpha=1)
print(x,y)
for i in range(0,len(y)):
plt.text(x[i],y[i],y[i], ha='center', bbox=dict(alpha=0.2))
plt.show()