使用pandas结合条形图和折线图

2024-10-04 11:30:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把条形图和折线图结合起来,但我好像搞不懂。我试过代码here,但没有成功。我有以下代码和数据帧:

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

import matplotlib
matplotlib.style.use('ggplot')

%matplotlib inline

dates = pd.date_range('2017-09-16',periods=11)

df = pd.DataFrame({'Elektra_Cost': pd.Series([1.483393,                                                
                                           1.483393,
                                           1.483393,
                                           1.481280,
                                           1.470714,
                                           1.470714,
                                           1.470714,
                                           1.506829,
                                           1.677233,
                                           1.721489,
                                           1.766318], index=dates, dtype='float64'), 
                'Gas_Cost': pd.Series([0.82122857, 
                                        0.82122857, 
                                        0.82122857, 
                                        0.85281429, 
                                        1.01074286, 
                                        1.01074286, 
                                        1.01074286,  
                                        0.92651429,  
                                        1.04047059,  
                                        1.50217941, 
                                        0.58479348],index=dates,dtype='float64'),
                'TG10': pd.Series([10.3, 
                                   11.0,
                                   11.3,
                                   12.0,  
                                   13.0,
                                   13.1,
                                   12.8,
                                   11.1,  
                                   13.5,
                                   14.1,  
                                   13.3],index=dates,dtype='float64'), 
                'TN10': pd.Series([5.8, 
                                   4.3, 
                                   9.0,
                                   7.5, 
                                   8.2,
                                   7.9, 
                                   6.0, 
                                   4.3, 
                                   4.6, 
                                   8.5, 
                                   8.8],index=dates,dtype='float64'), 
                'TX10': pd.Series([15.7,  
                                   17.3,  
                                   15.4,  
                                   17.3,  
                                   18.5,  
                                   19.2,  
                                   20.0,  
                                   18.2,  
                                   20.6,
                                   18.9,  
                                   18.2],index=dates,dtype='float64'),

               })

然后我试着画出这样的图:

^{pr2}$

结果如下图:

plot

你知道为什么我没有看到图表中的线条(TG10、TN10和TX10)吗?在

我在jupyter笔记本上运行这个

更新:

第一个建议的link做到了。所以我就这样做了:

fig = plt.figure()
ax = df[['Elektra_Cost', 'Gas_Cost']].plot(figsize=(20,15), kind='bar')
plt.xticks(rotation=0)
ax2 = ax.twinx()
ax2.plot(ax.get_xticks(), df[['TG10','TN10', 'TX10']], marker='o')
plt.show()

结果是:

plot


Tags: importdfindexplotmatplotlibaspltseries