How to plot two different graphs on same axes

285 views
Skip to first unread message

Richard Stanton

unread,
Oct 9, 2012, 5:05:16 PM10/9/12
to pyd...@googlegroups.com
I have two different dataframes that I'd like to plot simultaneously (they share an x-axis).

df1 I want to plot as a stacked bar chart, with labels for the different lines
df2 I want to plot as a line chart on the same axes, but using a different y-scale.

I can do either of these alone, but haven't been successful at getting them to plot simultaneously. Does anyone know how to do this?

Thanks a lot.

Richard Stanton

Chang She

unread,
Oct 9, 2012, 5:08:18 PM10/9/12
to pyd...@googlegroups.com
--



What do you get you when you try plotting them simultaneously? You're most likely running into this bug: https://github.com/pydata/pandas/issues/1800

I'll put in a fix for it on master this week. Please stay tuned.

-Chang

Richard Stanton

unread,
Oct 10, 2012, 2:40:08 PM10/10/12
to pyd...@googlegroups.com
>
>
>Richard Stanton <sta...@haas.berkeley.edu>
> Oct 09 02:05PM -0700
>
>
>
>
>I have two different dataframes that I'd like to plot simultaneously
>(they share an x-axis).
>
>
>
>
>
>df1 I want to plot as a stacked bar chart, with labels for the different
>lines
>
>
>df2 I want to plot as a line chart on the same axes, but using a
>different y-scale.
>
>
>
>
>
>
>I can do either of these alone, but haven't been successful at getting
>them to plot simultaneously. Does anyone know how to do this?
>
>
>
>
>Thanks a lot.
>
>
>
>
>
>Richard Stanton
>
>
>
>
>
>Chang She <ch...@lambdafoundry.com>
> Oct 09 05:08PM -0400
>
>
>
>
>
>
>> Thanks a lot.
>
>
>
>
>
>> Richard Stanton
>
>
>
>
>
>> --
>
>
>
>
>
>What do you get you when you try plotting them simultaneously? You're
>most likely running into this bug:
>
>https://github.com/pydata/pandas/issues/1800
>


It certainly sounds relevant, but I don't necessarily get an error
message. For example, when I try

df1 = DataFrame({'Series1' : range(9)}, index = range(2000,2009))
df2 = DataFrame({'Series2' : 5}, index = range(2000,2009))
df1.plot(kind='bar')
df2.plot(secondary_y=True)
draw()


I get two plots, one after the other. The second (line) plot has an x
range of 0 - 2500, so the entire graph is really compressed!

If I make the index range(9), now the axes look better (still two separate
graphs, though), but even if they were on the same axes they wouldn't
quite work as the spacing of the x-axis labels is different. On the bar
chart, there's a gap between the left/right hand side of the box and the
leftmost/rightmost tick. On the line graph, there's no such gap.

Richard



Richard Stanton

unread,
Oct 10, 2012, 2:56:38 PM10/10/12
to pyd...@googlegroups.com
>
>Wouter Overmeire <lod...@gmail.com>
> Oct 09 08:41PM +0200
>This will give you 145 different colors.
>
>
>
>
>
>In [45]: from matplotlib.colors import cnames
>In [46]: df.plot(style=cnames.keys())

Thanks, Wouter.
>
>

Wouter Overmeire

unread,
Oct 10, 2012, 3:38:17 PM10/10/12
to pyd...@googlegroups.com


2012/10/10 Richard Stanton <sta...@haas.berkeley.edu>
Sometimes i combine a boxplot and lineplot which has by default the same issue. Boxplots are plotted at positions 1, 2, 3, ...
matplotlib boxplot offers the argument positions, if used the boxes are created at the desired positions (i use the frame index values) instead of default 1, 2, 3, ...  When adding the lineplot, it nicely overlay`s with the boxes.

matplotlib bar does not offer the positions option :-( 

Anyway this seems an interesting use case for pandas to fix.

Note that if you want to add a horizontal line you can do this with axhline().

In [57]: ax = df1.plot(kind='bar')

In [58]: ax.axhline(5)
Out[58]: <matplotlib.lines.Line2D at 0xbd2e22c>

In [59]: draw()




Alvaro Tejero Cantero

unread,
Oct 19, 2012, 9:12:29 AM10/19/12
to pyd...@googlegroups.com
Hi,

FWIW I just tried style=cnames.keys() with df.plot(kind='barh',
stacked=True); cycling colors does not work there

-á.
> --
>
>

Wouter Overmeire

unread,
Oct 19, 2012, 9:18:04 AM10/19/12
to pyd...@googlegroups.com


2012/10/19 Alvaro Tejero Cantero <alv...@minin.es>
--



Can you show the exact command you used?

Alvaro Tejero Cantero

unread,
Oct 19, 2012, 9:19:48 AM10/19/12
to pyd...@googlegroups.com
from matplotlib.colors import cnames
blocks.apply(equip_units, axis=1).fillna(0).plot(kind='barh',
stacked=True, style=cnames.keys()).set_xlabel('number'); plt.show()


-á.
> --
>
>

Chang She

unread,
Oct 19, 2012, 9:29:46 AM10/19/12
to pyd...@googlegroups.com
If you do `color=cnames.keys()` it works for barh but `style=cnames.keys()` for line.
I made a bug report on github, we'll make it work for both parameters: https://github.com/pydata/pandas/issues/2082

--



Alvaro Tejero Cantero

unread,
Oct 19, 2012, 9:42:27 AM10/19/12
to pyd...@googlegroups.com
Fantastic, thank you Chang!

color did the trick. As a note, since there's so much of automatic
plotting happening in Pandas, one might want to predefine subsets of
cnames (discrete/categorical colormaps?) that are a bit more
orthogonal. I tried a few 6-element slices of cnames and always found
two nearly identical colors among the 6.


-á.
> --
>
>
Reply all
Reply to author
Forward
0 new messages