matplotlib基本使用

matplotlib的基本使用

1、安装matplotlib

  • pip install matplotlib

2、绘制折线图

from matplotlib import pyplot as plt

x = range(0, 10)
y = [15, 23, 2, 3, 4, 5, 6, 13, 2, 32]

# 设置图片大小
plt.figure(figsize=(10, 8), dpi=60)
# 绘图函数
plt.plot(x, y)
# 展示函数
plt.show()

  • 设置x轴或者y轴的刻度 plt.xticks(arg),plt.yticks(arg),arg可以是一个序列
    • plt.xticks(x)
    • plt.xticks(range(1,10,3))
    • x,y轴还支持字符串显示,
    • matplotlib默认不支持中文字体需要修改matplotlib的字体
#显示中文方式一
plt.rc("font",family="STSong")
xtick_labels=["10点{}分".format(i) for i in range(10)]
# 设置x轴刻度
# rotation标签旋转的度数
# fontProperties 显示中文方式二
plt.xticks(x,xtick_labels,rotation=45,fontProperties="STSong")
  • 添加描述信息
# 添加描述信息
plt.xlabel("时间")
plt.ylabel("温度 单位(℃)")
plt.title("十点到十点十分的温度变化")

  • 保存到本地
plt.savefig("./a.svg")
全部评论

相关推荐

在考古的丘比特很冷艳:[牛泪我横向一周多自动挂了,面试答的还行,感觉挂的莫名其妙
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务