使用matplotlib绘制直方图

使用matplotlib绘制直方图

from matplotlib import pyplot as plt

x = range(2, 26, 2)
y = [15, 13, 14, 5 ,17, 20 ,25, 26, 26, 24, 22, 18, 15]

plt.plot(x,y)
plt.show()

然后系统报错说x和y 不在一个维度,是因为y列表多了一个值,
去掉即可

生成一个这么样子的图:
在这里插入图片描述

from matplotlib import pyplot as plt

x = range(2,26,2)
y = [15,13,14,5,17,20,25,26,26,24,22,18]
plt.figure(figsize=(20,8), dpi=80) #设置生成的plot图大小和像素
plt.xticks(range(2,27))            #设置x轴刻度
plt.yticks(range(min(y), max(y)+1))  #设置y轴刻度

plt.plot(x,y)
plt.show()

效果图
在这里插入图片描述

from matplotlib import pyplot as plt
import random

x = range(0,120)
y = [random.randint(20,35) for i in range(120)]  #生成120个y轴随机数
plt.figure(figsize=(20,8), dpi=80)

plt.plot(x,y)
plt.show()

result

在这里插入图片描述

自定义刻度,并且支持中文显示

from matplotlib import pyplot as plt
import random
from matplotlib import font_manager

#设置字体路径, 打开文件夹C:/windows/fonts可查看
my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\STFANGSO.TTF")
x = range(0,120)
y = [random.randint(20,35) for i in range(120)]
plt.figure(figsize=(20,8), dpi=80)
_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i) for i in range(60)]
plt.xticks(list(x)[::3], _xtick_labels[::3], rotation=45, fontproperties=my_font)

plt.plot(x,y)
plt.show()

result
在这里插入图片描述

from matplotlib import pyplot as plt
import random
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\STFANGSO.TTF")
x = range(0,120)
y = [random.randint(20,35) for i in range(120)]
plt.figure(figsize=(20,8), dpi=80)
_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i) for i in range(60)]
plt.xticks(list(x)[::3], _xtick_labels[::3], rotation=45, fontproperties=my_font)

plt.xlabel("时间", fontproperties = my_font)
plt.ylabel("温度 单位(℃)", fontproperties = my_font)
plt.title("10点到12点每分钟的气温变化情况", fontproperties = my_font)

plt.plot(x,y)
plt.show()

result
在这里插入图片描述

还可以设置多条折线,图例,线段类型等多种样式
在这里插入图片描述
result
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

weixin_37539225

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

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

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

打赏作者

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

抵扣说明:

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

余额充值