matplotlib 常用方法 (柱状图,柱线混合图等)

阅读: 评论:0

matplotlib 常用方法 (柱状图,柱线混合图等)

matplotlib 常用方法 (柱状图,柱线混合图等)

matplotlib常用方法总结

中文显示

import matplotlib
Params['font.sans-serif'] = ['SimHei']  # 用黑体显示中文

柱状图

data= [4.9,4.6,3.2,3.4,3.5,4.3,4.5,5.0]
labels = ['唯品会', '网易严选', '拼多多', '京东', '淘宝',"阿里巴巴","苏宁易购","必要"]
plt.title("电商平台下载量")
plt.xlabel("电商平台")
plt.ylabel("下载次数/万次")
plt.bar(range(len(data)), data, tick_label=labels,width=0.6,alpha=0.8)
plt.show()

扇形图

plt.figure(figsize=(6,9)) #调节图形大小
labels = ["上海","江苏","浙江","安徽","福建","江西","山东"] 
#定义标签
sizes = [440696, 2053630, 1545153, 813279,702826,454387,1801301]
# 定义值
# colors = ['green', 'yellow', 'blue', 'red'] 
# explode = (0,0,0,0) 
#将某一块分割出来,值越大分割出的间隙越大patches,text1,text2 = plt.pie(sizes,#   explode=explode,labels=labels,#   colors=colors,autopct = '%3.2f%%', #数值保留固定小数位shadow = False, #无阴影设置startangle =90, #逆时针起始角度设置pctdistance = 0.6) #数值距圆心半径倍数距离
#patches饼图的返回值,texts1饼图外label的文本,texts2饼图内部的文本
# x,y轴刻度设置一致,保证饼图为圆形
plt.axis('equal')
plt.legend() # 右上角显示
plt.title("华中地区企业占比")
plt.show()

柱线混合,不同y轴

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()years =  [x for x in range(2010,2020) ]
graduate_num = [662,682,689,700,721,738,749,762,791,915]
school_num = [x for x in range(2358,2688,25)]
school_num[3:7]=[]
# 设置标题
plt.title("全国高等院校数及学生统计数")
# 为两条坐标轴设置名称
plt.xlabel("年份")
ax1.set_ylim([600,1000])
ax1.set_ylabel("学生数 /万人")
ax2.set_ylabel("高等院校数")# 双Y轴标签颜色设置
ax2.yaxis.label.set_color("red")# 双Y轴刻度颜色设置
ax2.tick_params(axis = 'y', colors = "red")for a, b in zip(years, school_num):(a, b, b, ha='center', va='bottom',fontsize=8)
ax1.bar(years, graduate_num, width=0.4,alpha=0.7)
ax2.plot(years, school_num,color='red',alpha=0.3,linestyle='--')
plt.show()

方法就是sub子图,对不同子图画不同的图形,改变y轴坐标

本文发布于:2024-02-01 02:32:08,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170672593033226.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:常用   方法   柱状图   matplotlib
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23