【稳住,can win】Matplotlib基本操作

Matplotlib 绘制基本图形

# 导入 matplotlib
from matplotlib import pyplot as plt

x = range(2, 26, 2)
y = [15,13,14.5,17,20,25,26,26,27,22,18,15]

# 绘图
plt.plot(x, y)

# 展示
plt.show()

在这里插入图片描述
可以看到绘制的图形坐标轴的设置是Matplotlib自动帮我们设定的。但是,我们也可以自己调整坐标轴以及图像大小等属性。

调整图像属性

调整图像大小


plt.figure(figsize=(20, 12), dpi=80) # figsize调整图像大小,dpi调整图像清晰度

调整坐标轴
plt.xticks(x) # 传入 x , x 轴上显示的刻度都是 x 的值

在这里插入图片描述
当传入的刻度长度过长时,会出现下图的情况。
在这里插入图片描述

这时可以调整步距,从而调整刻度的稀疏程度。

在这里插入图片描述

Matplotlib 绘制2小时气温走势

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

# 设置中文字体
font = font_manager.FontProperties(fname="/usr/share/fonts/truetype/arphic-gbsn00lp/gbsn00lp.ttf")

x = range(0, 120)
y = [random.randint(20,35) for i in range(120)]

_x = list(x)
_x_label = ["10点{}分".format(i) for i in range(60)]
_x_label += ["11点{}分".format(i) for i in range(60)]


plt.plot(x, y)
plt.xticks(_x[::5], _x_label[::5], fontproperties=font, rotation=45)
# 添加描述信息
plt.xlabel("时间", fontproperties=font)
plt.ylabel("温度 单位(℃)", fontproperties=font)
plt.title("10点到12点气温变化", fontproperties=font)

plt.show()

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Schuyler Hu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值