matplotlib.pyplot用法,扩展库matplotlib.pyplot中的函数plot(matplotlib.pyplot库没办法用)

matplotlib.pyplot是Python中一个非常常用的绘图库,用于绘制各种类型的图表,下面是关于matplotlib.pyplot的用法的详细解释:

matplotlib.pyplot用法,扩展库matplotlib.pyplot中的函数plot(matplotlib.pyplot库没办法用)
(图片来源网络,侵删)

1、导入库:

import matplotlib.pyplot as plt

2、创建一个简单的折线图:

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.show()

3、添加标题和轴标签:

plt.plot(x, y)
plt.title("简单折线图")
plt.xlabel("X轴")
plt.ylabel("Y轴")
plt.show()

4、绘制多个线条:

x = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]
y2 = [1, 3, 5, 7, 9]
plt.plot(x, y1, label="线条1")
plt.plot(x, y2, label="线条2")
plt.legend()
plt.show()

5、绘制散点图:

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.scatter(x, y)
plt.show()

6、绘制柱状图:

x = ['A', 'B', 'C', 'D', 'E']
y = [3, 7, 2, 5, 8]
plt.bar(x, y)
plt.show()

7、绘制饼图:

labels = ['A', 'B', 'C', 'D', 'E']
sizes = [15, 30, 45, 10, 20]
plt.pie(sizes, labels=labels)
plt.show()

8、设置坐标轴范围:

plt.plot(x, y)
plt.xlim([0, 6]) # X轴范围为0到6
plt.ylim([0, 12]) # Y轴范围为0到12
plt.show()

9、保存图表为图片文件:

plt.plot(x, y)
plt.savefig("my_plot.png") # 保存为PNG格式的图片文件
plt.show()

原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/515386.html

(0)
未希的头像未希新媒体运营
上一篇 2024-04-24 22:16
下一篇 2024-04-24 22:18

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购  >>点击进入