如何在相同的图上显示条线图

时间:2021-06-24 12:02:13

I am unable to show a bar and line graph on the same plot. Example code:

我无法在同一块图上显示一个条形图和线形图。示例代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

Df = pd.DataFrame(data=np.random.randn(10,4), index=pd.DatetimeIndex(start='2005', freq='M', periods=10), columns=['A','B','C','D'])

fig = plt.figure()
ax = fig.add_subplot(111)

Df[['A','B']].plot(kind='bar', ax=ax)
Df[['C','D']].plot(ax=ax, color=['r', 'c'])

3 个解决方案

#1


12  

You can also try this:

你也可以试试这个:

fig = plt.figure()
ax = DF['A','B'].plot(kind="bar");plt.xticks(rotation=0)
ax2 = ax.twinx()
ax2.plot(ax.get_xticks(),DF['C','D'],marker='o')

#2


2  

I wanted to know as well, however all existing answers are not for showing bar and line graph on the same plot, but on different axis instead.

我也想知道,但是所有现有的答案都不是在同一个图上显示条线,而是在不同的轴上。

so I looked for the answer myself and have found an example that is working -- Plot Pandas DataFrame as Bar and Line on the same one chart. I can confirm that it works.

所以我自己找了答案,找到了一个行之有效的例子——在同一个图表上画出熊猫数据爆炸图。我可以确认它是有效的。

What baffled me was that, the almost same code works there but does not work here. I.e., I copied the OP's code and can verify that it is not working as expected.

令我困惑的是,几乎相同的代码在那里可以工作,但在这里不能工作。即。,我复制了OP的代码,可以验证它是否按预期工作。

The only thing I could think of is to add the index column to Df[['A','B']] and Df[['C','D']], but I don't know how since the index column doesn't have a name for me to add.

我唯一能想到的就是将索引列添加到Df[['A','B']]和Df[[C','D']],但我不知道为什么索引列没有我要添加的名称。

Today, I realize that even I can make it works, the real problem is that Df[['A','B']] gives a grouped (clustered) bar chart, but grouped (clustered) line chart is not supported.

今天,我意识到即使我可以让它工作,真正的问题是Df[['A','B']]给出了一个分组(集群)条形图,但不支持分组(集群)线形图。

#3


0  

You can do something like that, both on the same figure:

你可以做这样的事情,两个数字都是一样的:

In [4]: Df = pd.DataFrame(data=np.random.randn(10,4), index=pd.DatetimeIndex(start='2005', freq='M', periods=10), columns=['A','B','C','D'])

In [5]: fig, ax = plt.subplots(2, 1) # you can pass sharex=True, sharey=True if you want to share axes.

In [6]: Df[['A','B']].plot(kind='bar', ax=ax[0])
Out[6]: <matplotlib.axes.AxesSubplot at 0x10cf011d0>

In [7]: Df[['C','D']].plot(color=['r', 'c'], ax=ax[1])
Out[7]: <matplotlib.axes.AxesSubplot at 0x10a656ed0>

#1


12  

You can also try this:

你也可以试试这个:

fig = plt.figure()
ax = DF['A','B'].plot(kind="bar");plt.xticks(rotation=0)
ax2 = ax.twinx()
ax2.plot(ax.get_xticks(),DF['C','D'],marker='o')

#2


2  

I wanted to know as well, however all existing answers are not for showing bar and line graph on the same plot, but on different axis instead.

我也想知道,但是所有现有的答案都不是在同一个图上显示条线,而是在不同的轴上。

so I looked for the answer myself and have found an example that is working -- Plot Pandas DataFrame as Bar and Line on the same one chart. I can confirm that it works.

所以我自己找了答案,找到了一个行之有效的例子——在同一个图表上画出熊猫数据爆炸图。我可以确认它是有效的。

What baffled me was that, the almost same code works there but does not work here. I.e., I copied the OP's code and can verify that it is not working as expected.

令我困惑的是,几乎相同的代码在那里可以工作,但在这里不能工作。即。,我复制了OP的代码,可以验证它是否按预期工作。

The only thing I could think of is to add the index column to Df[['A','B']] and Df[['C','D']], but I don't know how since the index column doesn't have a name for me to add.

我唯一能想到的就是将索引列添加到Df[['A','B']]和Df[[C','D']],但我不知道为什么索引列没有我要添加的名称。

Today, I realize that even I can make it works, the real problem is that Df[['A','B']] gives a grouped (clustered) bar chart, but grouped (clustered) line chart is not supported.

今天,我意识到即使我可以让它工作,真正的问题是Df[['A','B']]给出了一个分组(集群)条形图,但不支持分组(集群)线形图。

#3


0  

You can do something like that, both on the same figure:

你可以做这样的事情,两个数字都是一样的:

In [4]: Df = pd.DataFrame(data=np.random.randn(10,4), index=pd.DatetimeIndex(start='2005', freq='M', periods=10), columns=['A','B','C','D'])

In [5]: fig, ax = plt.subplots(2, 1) # you can pass sharex=True, sharey=True if you want to share axes.

In [6]: Df[['A','B']].plot(kind='bar', ax=ax[0])
Out[6]: <matplotlib.axes.AxesSubplot at 0x10cf011d0>

In [7]: Df[['C','D']].plot(color=['r', 'c'], ax=ax[1])
Out[7]: <matplotlib.axes.AxesSubplot at 0x10a656ed0>