用python帮我画各种形式的对比图
好的,以下是几种对比图的Python代码示例:
- 柱形图
import matplotlib.pyplot as plt
# x和y的值
x = [1, 2, 3, 4, 5]
y1 = [10, 8, 6, 4, 2]
y2 = [2, 4, 6, 8, 10]
# 画柱形图
plt.bar(x, y1, align='center')
plt.bar(x, y2, align='center')
# 标题和标签
plt.title('Bar Chart')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# 显示图形
plt.show()
- 折线图
import matplotlib.pyplot as plt
# x和y的值
x = [1, 2, 3, 4, 5]
y1 = [10, 8, 6, 4, 2]
y2 = [2, 4, 6, 8, 10]
# 画折线图
plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
# 标题和标签
plt.title('Line Chart')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# 添加图例
plt.legend()
# 显示图形
plt.show()
- 散点图
import matplotlib.pyplot as plt
# x和y的值
x1 = [1, 2, 3, 4, 5]
y1 = [10, 8, 6, 4, 2]
x2 = [2, 4, 6, 8, 10]
y2 = [5, 4, 3, 2, 1]
# 画散点图
plt.scatter(x1, y1, label='Group 1')
plt.scatter(x2, y2, label='Group 2')
# 标题和标签
plt.title('Scatter Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# 添加图例
plt.legend()
# 显示图形
plt.show()
- 饼图
import matplotlib.pyplot as plt
# 饼图数据
sizes = [20, 30, 40, 10]
labels = ['Group A', 'Group B', 'Group C', 'Group D']
colors = ['red', 'blue', 'green', 'orange']
# 画饼图
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
# 标题
plt.title('Pie Chart')
# 显示图形
plt.show()
以上是一些常见的对比图形式的Python示例,你可以根据自己的需求修改和扩展
原文地址: https://www.cveoy.top/t/topic/fck6 著作权归作者所有。请勿转载和采集!