1

I have a DF with grouped data such as:

fieldsX = ['Year_start','Week_start']
f = { 'Integrado':['count'], 'Duration_hours':['min','mean','max'] }
a = a.groupby(fieldsX).agg(f)

This is a snippet of the DF...

                       Duration_hours                      Integrado
                                  min        mean     max      count
Year_start Week_start                                               
2017       1                    428.9  428.900000   428.9          1
           2                      0.5   19.566667   119.8          9
           3                      0.9   14.112500    96.4         16
           4                      1.5    2.850000     4.2          2
           5                      1.8    3.540000     5.5          5
           6                      0.7    2.242857     6.2          7
           7                      0.4  113.075000   696.2          8
           8                      1.0  163.375000  1158.5          8

I'm trying to plot this, using the secondary_y [a] axis for the Integrado field.

ax = a.plot(kind='bar', legend=True, fontsize=8, figsize=(15, 10), secondary_y=['Integrado'])

But I cannot achieve it. All the data is using the y axis on the left.

enter image description here

As per [a] and [b] I should declare a list including which columns should be using the secondary axis, but it's now working.

UPDATE: following this, I've managed to use the secondary axis. Have a look.

enter image description here

The trick is to create a fig object and a second ax2 axis object.

fig = plt.figure()
ax  = fig.add_subplot(111)
ax2 = ax.twinx()
a.Integrado.plot(kind='bar',ax=ax)
a.Duration_hours.plot(kind='line',ax=ax2)

The problem now is that on the x-axis, the data (year,week) is gone, in comparison to the previous plot.

1) Ideas on how to have the x-axis information?

2) Why is the parameter secondary_y not working at all?

Thanks!

[a] - https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html

[b] - http://pandas.pydata.org/pandas-docs/version/0.21/visualization.html

Lucas Aimaretto
  • 1,399
  • 1
  • 22
  • 34
  • Post clean data before groupby for test – Serenity Dec 22 '17 at 08:05
  • 1
    A pandas bar plot is a categorical plot. It is shown in the range 0...N-1 where N is the number of bars. The line plot is a numeric plot. It is shown on the data range between the minimum and maximum x value of the data. I would guess the problem is the same as [this one](https://stackoverflow.com/questions/33457861/pandas-dataframe-how-to-mix-bar-and-line-plots-with-different-scales) and we can close as dubplicate. An alternative to the shown solution would be to use a matplotlib plot (instead of pandas) because a matplotlib bar plot also uses numeric x values. – ImportanceOfBeingErnest Dec 22 '17 at 08:23
  • This question is not the same as closed because of complex index – Serenity Dec 22 '17 at 12:33
  • Hi @ImportanceOfBeingErnest, thanks for the link!. I had to tune the x-axis with `ax1.set_xticklabels(a.index)`. That did the trick. I'll include the answer. Thanks! – Lucas Aimaretto Dec 22 '17 at 19:23

0 Answers0