matplotlib中折线图的简单使用及美化

气温随时间变化的规律

为寻找当地气温随时间变化的规律,对四十天内气温的变化进行统计得到如下数组:
[32, 25, 15, 19, 18, 23, 27, 23, 24, 24, 24, 25, 17, 17, 17, 22, 26, 27, 28, 28, 20, 17, 19, 19, 23, 19, 19, 23, 24, 26, 27, 28, 28, 29, 26, 27, 29, 30, 29, 29]

from matplotlib import pyplot as plt
import matplotlib
matplotlib.rc("font",family='FangSong')

temperature_1 = [32, 25, 15, 19, 18, 23, 27, 23, 24, 24, 24, 25, 17, 17, 17, 22, 26, 27, 28, 28, 20, 17, 19, 19, 23, 19, 19, 23, 24, 26, 27, 28, 28, 29, 26, 27, 29, 30, 29, 29]
temperature_2 = [24, 17, 15, 20, 18, 21, 23, 23, 23, 23, 24, 22, 17, 18, 17, 20, 24, 27, 29, 28, 17, 17, 21, 22, 25, 21, 20, 23, 25, 23, 27, 29, 29, 28, 24, 26, 29, 30, 27, 25]
count = 0
for i in temperature_1:
    count +=1

x = range(1,count+1,1)
y_1 = temperature_1
y_2 = temperature_2

_xticks_labels = ["第{}日".format(i) for i in x]
plt.xticks(x[::3], _xticks_labels[::3], rotation=45)

plt.grid(alpha=0.3)
plt.scatter(x, y_1, label="临汾")
plt.scatter(x, y_2, label="太原")
plt.legend()

plt.xlabel("时间", loc="right")
plt.ylabel("温度(单位:摄氏度)", loc="top")
plt.title("四十日两地气温变化散点图")
plt.subplots_adjust(left=0.1, right=0.97, top=0.95, bottom=0.15) 

plt.savefig("./scatter.png")

plt.show()
  • 绘制命令为plt.scatter()
  • plt.subplots_adjust(left=0.1, right=0.97, top=0.95, bottom=0.15) :
    • 根据需要对图片内容与边缘的距离进行调整,数值范围为0~1;
    • 距离参数以左边框与下边框为基准;

该代码运行结果为:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

世澈

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

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

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

打赏作者

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

抵扣说明:

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

余额充值