python 从原点开始画双列柱状图

感谢chatgpt

import matplotlib.pyplot as plt

# Define the data for the two columns
column1 = [1, 2, 3, 4, 5]
column2 = [2, 4, 6, 8, 10]

# Define the x-axis labels
x_labels = [0, 1, 2, 3, 4]

# Plot the two columns of data as a bar chart
fig, ax = plt.subplots()
ax.bar(x_labels, column1, label='Column 1')
ax.bar(x_labels, column2, label='Column 2', bottom=column1)

# Add a legend and labels for the x and y axes
ax.legend()
ax.set_xlabel('X Value')
ax.set_ylabel('Y Value')

# Set the limits of the x and y axes to start at 0
ax.set_xlim(0, max(x_labels))
ax.set_ylim(0, max(column1 + column2))

# Display the bar chart
plt.show()

在上面的代码中,我们定义了两个数列的数据,并定义了 x 轴的标签。然后,我们使用 Matplotlib 库绘制双列柱状图,并在图上添加标题、图例、坐标轴标签。最后,我们使用 ax.set_xlim(0, max(x_labels)) 和 ax.set_ylim(0, max(column1 + column2)) 函数设置 x 轴和 y 轴的范围,从而确保图形从原点开始画。最后,我们使用 plt.show() 函数显示图形。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值