'matplotlib bar & line chart with date
I'm trying to create a chart with both bar and line. when the axis is number, I can do it use this code
ax=tmp[['str1','net']].plot(x='str1',kind='bar')
tmp[['str1','cum']].plot(x='str1',kind='line',ax=ax)
plt.show()
but I want to use date in x-axis, this code stops working. there is only the bar chart.
ax=tmp[['date','net']].plot(x='date',kind='bar')
tmp[['date','cum']].plot(x='date',kind='line',ax=ax)
plt.show()
I found in a post suggests add "use_index=False". If I add this to the 2nd plot, the line and bar are there but there is some overlapping in label
ax=tmp[['date','net']].plot(x='date',kind='bar')
tmp[['date','cum']].plot(x='date',kind='line',ax=ax,use_index=False)
plt.show()
If I add use_index=False to both plots, the x-axis label is not used .

I'm also seeing this warning of "This figure includes Axes that are not compatible". Not sure if it's related.
the test data is created by the code below. thank you guys in advance.
dict_df={'date': {0: pd.to_datetime('2022-03-21 00:00:00'),
1: pd.to_datetime('2022-04-11 00:00:00'),
2: pd.to_datetime('2022-04-12 00:00:00'),
3: pd.to_datetime('2022-04-13 00:00:00'),
4: pd.to_datetime('2022-04-14 00:00:00'),
5: pd.to_datetime('2022-04-15 00:00:00'),
6: pd.to_datetime('2022-04-16 00:00:00')},
'net': {0: 25.360000000000014,
1: -9607.43,
2: -4879.459999999999,
3: -11028.11,
4: -5295.470000000001,
5: -1458.3600000000006,
6: 3631.3499999999995},
'cum': {0: 25.360000000000014,
1: -9582.07,
2: -14461.529999999999,
3: -25489.64,
4: -30785.11,
5: -32243.47,
6: -28612.120000000003},
'str1': {0: 7, 1: 6, 2: 5, 3: 4, 4: 3, 5: 2, 6: 1}}
tmp=pd.DataFrame(dict_df)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|



