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'])
Create a combo chart with a secondary axisClick anywhere in the chart you want to change to a combo chart to show the CHART TOOLS. Click DESIGN > Change Chart Type. On the All Charts tab, choose Combo, and then pick the Clustered Column - Line on Secondary Axis chart.
Select the specified bar you need to display as a line in the chart, and then click Design > Change Chart Type. See screenshot: 3. In the Change Chart Type dialog box, please select Clustered Column – Line in the Combo section under All Charts tab, and then click the OK button.
Use a Line-Column Chart to Display Two Varying Sets of Data A line-column chart combines a line graph and column chart on the same graph. The two charts share an X axis but each has its own Y axis. There are two common uses for a combination chart: 1. when want to display two different data sets together and 2.
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')
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With