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() 函数显示图形。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 Pandas 和 Matplotlib 模块来读取 Excel 文件,并出双向横向柱。具体代码如下: import pandas as pd import matplotlib.pyplot as plt # 读取 Excel 文件 df = pd.read_excel('data.xlsx') # 双向横向柱 df.plot(kind='barh', x='x', y=['y1','y2'], stacked=True) # 显示表 plt.show() ### 回答2: 要使用Python读取Excel并绘制双向横向柱,可以使用pandas和matplotlib库来完成。 首先,确保已经安装了pandas和matplotlib库。可以使用以下命令安装所需库: ```python pip install pandas pip install matplotlib ``` 接下来,使用pandas库的`read_excel`函数读取Excel文件并将数据存储到一个DataFrame对象中。假设要读取的Excel文件名为"example.xlsx",其中包含两列数据,第一列为柱的横坐标,第二列为柱的数据。可以使用以下代码进行读取: ```python import pandas as pd df = pd.read_excel('example.xlsx') ``` 然后,使用matplotlib库绘制双向横向柱。可以使用matplotlib的`barh`函数来绘制柱。为了实现双向柱的效果,可以先绘制正向柱,然后再绘制反向柱。以下是一个示例代码,其中假设正向和反向柱的颜色分别为红色和蓝色: ```python import matplotlib.pyplot as plt plt.barh(df['横坐标'], df['数据'], color='red') plt.barh(df['横坐标'], -df['数据'], color='blue') plt.show() ``` 最后,使用`show`函数显示绘制的双向横向柱。 请注意,以上代码仅是一个示例,具体实现可能会根据Excel数据的结构和需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值