0

I'm trying to combine a bar chart and a line chart but I can't seem to figure it out. I tried the code here but to no reveil. I have the following code and Dataframe:

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'),

               })

I then try to plot the graph like this:

ax = df[['TG10', 'TN10', 'TX10']].plot(figsize=(20,15), linestyle='--', secondary_y=['TG10', 'TN10', 'TX10'])
df[['Elektra_Cost', 'Gas_Cost']].plot(figsize=(20,15), kind='bar', ax=ax)
plt.show()

Which results in the following graph:

plot

Any ideas why I'm not seeing the lines (TG10, TN10 and TX10) in the chart?

I'm running this in jupyter notebook

Update:

The first suggested link did the trick. So I ended up doing this:

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()

Which resulted in:

plot

Tijmen
  • 41
  • 1
  • 3
  • Oh, they're there all right. It's just that your scale is off. Your costs are < 2, your T* columns are mostly over 10. Either adjust your scale, or draw in subplots. – cs95 Sep 30 '17 at 13:16
  • @coldspeed there are 2 y axes so it should be visible. What is the OP missing? If there was one, and values with multiple orders of magnitude difference, that comment makes sense. As it is, I don't see how it is so. – roganjosh Sep 30 '17 at 13:22
  • 1
    "What is the OP missing?" A [mcve] of the issue! – ImportanceOfBeingErnest Sep 30 '17 at 13:26
  • The actual problem is not clear; the example is not verifiable, hence I closed as duplicate of several other questions seeking to plot a bar and a line plot in the same chart with dates. If the question makes it clear in how far the answers to those questions are not helping, it can potentially be reopened. – ImportanceOfBeingErnest Sep 30 '17 at 13:53
  • @ImportanceOfBeingErnest, it should be more clear now. – Tijmen Sep 30 '17 at 14:15
  • Did you not try any of the linked solutions? At the moment I don't see in how far they would fail here. – ImportanceOfBeingErnest Sep 30 '17 at 14:33
  • @ImportanceOfBeingErnest, missed that. Will try and let you know. thanks – Tijmen Sep 30 '17 at 14:40
  • The linked solutions in the yellow box on top (which also appear next to the question under the headline "Linked"). – ImportanceOfBeingErnest Sep 30 '17 at 14:42

0 Answers0