matplotlib使用教程

本文介绍了如何使用Python的matplotlib库绘制折线图、散点图和条形图。示例代码详细展示了创建各种图形的过程,包括设置轴标签、网格线和图例等。通过实例,读者可以学习到如何自定义图形属性,以实现专业图表的制作。
摘要由CSDN通过智能技术生成

b站网课
官方文档
另一个好用的在线画图工具是百度的echarts
plotly python

1.绘制折线图

from matplotlib import pyplot as plt
import matplotlib

x = range(2, 28, 2)
# x = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26]
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 24, 22, 18, 15]
y_2 = [i / 2 for i in y]

my_font = {'family': 'monospace',
           'weight': 'bold',
           'size': 20}
plt.rc('font', **my_font)

# dpi:dots per inch
fig = plt.figure(figsize=(20, 10), dpi=80)

plt.plot(x, y, label="curve1", color="red", ls=':', lw='5')
plt.plot(x, y_2, label="curve2", color="green", ls='-.')

# set x and y axis
# plt.xticks(x)
# plt.xticks(range(0, 30, 1))
_xticks_label = ["hello,{}".format(i) for i in x]
plt.xticks(x, _xticks_label, rotation=45)
plt.yticks(range(0, 30, 1))

plt.xlabel("x axis", fontproperties=my_font)
plt.ylabel("y axis", fontproperties=my_font)
plt.title("title", fontproperties=my_font)

# grid and its transparency
plt.grid(alpha=0.25, ls='--')

plt.legend(prop=my_font, loc="best")

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

plt.show()

在这里插入图片描述

2.绘制散点图

plt.scatter(x, y, label="curve2", color="red")
plt.scatter(x, y_2, label="curve2", color="green")

3.绘制条形图

纵向

plt.bar(x, y, label="curve2", color="red", width=0.4)
plt.bar(x, y_2, label="curve2", color="green", width=0.2)

横向

plt.barh(x, y, label="curve2", color="red",height=0.4)
plt.barh(x, y_2, label="curve2", color="green")

绘制多个纵向条形图:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值