Matplotlib画图模块(第二篇)

1.Scatter方法

matplotlib模块内置的Scatter方法能够帮助我们将数据绘制出散点图

#统计3月和10月每天的气温变化,用散点图表示
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\STSONG.TTF")
y_3=[11,17,16,21,33,12,15,16,18,14,15,13,21,20,15,16,40,25,16,31,9,16,18,15,21,23,24,26,21,30]
y_10=[11,17,16,21,33,12,15,16,18,14,15,13,21,20,15,16,40,25,16,31,9,16,18,15,21,23,24,26,21,30]
x_3=range(1,31)
x_10=range(51,81)
plt.figure(figsize=(20,8),dpi=80)
plt.scatter(x_3,y_3,label="3月",color='orange')
plt.scatter(x_10,y_10,label="10月",color='purple')
_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,_xtick_labels,rotation=45,fontproperties=my_font)
plt.xlabel("月份",fontproperties=my_font)
plt.ylabel("气温",fontproperties=my_font)
plt.legend(prop=my_font,loc=2)
plt.show()

 2.Bar方法

matplotlib模块内置的bar方法可以帮助我们画出条形统计图

from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\STSONG.TTF")
x=["朝花夕誓","白蛇:缘起","青蛇:劫起","杨戬","重生:哪吒","秒速5厘米","你的名字","天气之子","玲芽之旅","我想吃掉你的胰脏","怦然心动","绿皮书","何以为家","大鱼海棠","哪吒之魔童降世"]
y=[36.15,42.56,53.88,41.98,42.76,43.51,44.68,55.76,39.12,58.61,77.92,82.56,65.37,56.85,77.66]
plt.figure(figsize=(20,8),dpi=80)
plt.bar(range(len(x)),y,width=0.3,color="yellow")
plt.xlabel("电影名称",fontproperties=my_font)
plt.ylabel("电影支持度",fontproperties=my_font)
plt.title("近期热门电影及其支持度统计",fontproperties=my_font)
plt.xticks(range(len(x)),x,rotation=45,fontproperties=my_font)
plt.show()

使用barh方法可以绘制出横向条形统计图(注意此时不能添加width因为该方法自带否则会报错):

from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\STSONG.TTF")
x=["朝花夕誓","白蛇:缘起","青蛇:劫起","杨戬","重生:哪吒","秒速5厘米","你的名字","天气之子","玲芽之旅","我想吃掉你的胰脏","怦然心动","绿皮书","何以为家","大鱼海棠","哪吒之魔童降世"]
y=[36.15,42.56,53.88,41.98,42.76,43.51,44.68,55.76,39.12,58.61,77.92,82.56,65.37,56.85,77.66]
plt.figure(figsize=(20,8),dpi=80)
plt.barh(range(len(x)),y,color="yellow")
plt.ylabel("电影名称",fontproperties=my_font)
plt.xlabel("电影支持度",fontproperties=my_font)
plt.title("近期热门电影及其支持度统计",fontproperties=my_font)
plt.yticks(range(len(x)),x,rotation=45,fontproperties=my_font)
plt.show()

 

 

 2.1.绘制多条条形统计图

from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\STSONG.TTF")
x=["战狼2","猩球崛起","迪迦奥特曼-最终圣战","星球大战"]
a_14=[42.35,55.64,87.66,45.65]
a_15=[44.56,58.64,92.18,55.66]
a_16=[43.78,54.66,99.99,56.87]
plt.figure(figsize=(20,8),dpi=80)
width_w=0.1
x_14=list(range(len(a_14)))
x_15=[i+width_w for i in x_14]#此处传入条形统计图宽度来保证图像的合理堆叠性
x_16=[i+width_w*2 for i in x_14]
plt.bar(range(len(x)),a_14,width=width_w,label="14号")
plt.bar(x_15,a_15,width=width_w,label="15号")
plt.bar(x_16,a_16,width=width_w,label="16号")
plt.legend(prop=my_font,loc=2)
plt.xticks(x_15,x,fontproperties=my_font)
plt.show()

 3.绘制频率分布直方图:

matplotlib模块也内置了hist方法来绘制频率分布直方图

#制作一个频率分布直方图来反映一组数据的变化情况
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\STSONG.TTF")
a=[20,16,58,33,12,23,44,42,35,36,25,41,36,32,21,23,33,36]
group_width=2
num_bins=(max(a)-min(a))//group_width
plt.figure(figsize=(20,8),dpi=80)
plt.hist(a,num_bins)
plt.xticks(range(min(a),max(a)+group_width,group_width))
plt.grid()
plt.show()

 ps:一般来说,能够使用hist方法的大多是那些没有经过处理过的数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愿能改写结局

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值