3

I have a dataframe which looks something like this:

   AgeGroups Factor Cancer  Frequency
0      00-05      B    Yes        223
1      00-05      A     No        108
2      00-05      A    Yes          0
3      00-05      B     No       6575
4      11-15      B    Yes        143
5      11-15      A     No          5
6      11-15      A    Yes          1
7      11-15      B     No       3669
8      16-20      B    Yes        395
9      16-20      A     No         28
10     16-20      A    Yes          1
11     16-20      B     No       6174
12     21-25      B    Yes        624
13     21-25      A     No         80
14     21-25      A    Yes          2
15     21-25      B     No       8173
16     26-30      B    Yes        968
17     26-30      A     No        110
18     26-30      A    Yes          2
19     26-30      B     No       9143
20     31-35      B    Yes       1225
21     31-35      A     No        171
22     31-35      A    Yes          5
23     31-35      B     No       9046
24     36-40      B    Yes       1475
25     36-40      A     No        338
26     36-40      A    Yes         21
27     36-40      B     No       8883
28     41-45      B    Yes       2533
29     41-45      A     No        782
..       ...    ...    ...        ...
54     71-75      A    Yes       2441
55     71-75      B     No      15992
56     76-80      B    Yes       4614
57     76-80      A     No       5634
58     76-80      A    Yes       1525
59     76-80      B     No      10531
60     81-85      B    Yes       1869
61     81-85      A     No       2893
62     81-85      A    Yes        702
63     81-85      B     No       5692
64     86-90      B    Yes        699
65     86-90      A     No       1398
66     86-90      A    Yes        239
67     86-90      B     No       3081
68     91-95      B    Yes        157
69     91-95      A     No        350
70     91-95      A    Yes         47
71     91-95      B     No       1107
72    96-100      B    Yes         31
73    96-100      A     No         35
74    96-100      A    Yes          2
75    96-100      B     No        230
76      >100      B    Yes          5
77      >100      A     No          1
78      >100      A    Yes          1
79      >100      B     No         30
80     06-10      B    Yes        112
81     06-10      A     No          6
82     06-10      A    Yes          0
83     06-10      B     No       2191

with the code:

by_factor = counts.groupby(level='Factor')


    k = by_factor.ngroups

    fig, axes = plt.subplots(1, k, sharex=True, sharey=False, figsize=(15, 8))
    for i, (gname, grp) in enumerate(by_factor):
        grp.xs(gname, level='Factor').plot.bar(
            stacked=True, rot=45, ax=axes[i], title=gname)

    fig.tight_layout()

I got a beautiful chart, which seems like this: enter image description here This actually served what I was looking for until I realized I wanted to re-adjust my y-axis in such a way that I could have same scale for y-axis in both of the charts. If you look at the right chart 'B', the y-axis has scale of 25000 and chart 'A' has the scale of 10000. Can anyone suggest what would be the best possible approach to have same scale on both charts?. I tried:

plt.ylim([0,25000])

which did rather nothing or didn't change anything in chart 'A' because this bascially only changes y-axis of chart 'B'.

I would highly appreciate any suggestion to achieve same scale for both plots.

Acerace.py
  • 679
  • 3
  • 9
  • 25
  • Possible duplicate of [Setting the same axis limits for all subplots in matplotlib](http://stackoverflow.com/questions/31006971/setting-the-same-axis-limits-for-all-subplots-in-matplotlib) – ImportanceOfBeingErnest Apr 23 '17 at 13:23

3 Answers3

5

Set ylim min and max values for every axis in a cycle:

for ax in axes: ax.set_ylim([0,25000])
Serenity
  • 35,289
  • 20
  • 120
  • 115
3

You may of course simply call .set_ylim() on the respective axes. The drawback of this is that you would need to know the limits to set by doing so. The following solutions do not have this requirement:

sharey

In your code you explicitely set sharey=False. If you change it to True, you get a shared yaxis. You can then use plt.ylim([0,25000]) to limit the axes, but you don't have to, since they are shared and will adjust automatically.

Minimal example:

import matplotlib.pyplot as plt

fig, (ax, ax2) = plt.subplots(ncols=2, sharex=True, sharey=True)

ax.plot([1,3,2])
ax2.plot([2,3,1])

plt.show()

enter image description here

As can be seen the ticklabels of the shared axes are hidden, which might be desirable in many cases.

join shared axes

Having two axes, you can make them sharing the same scale using

import matplotlib.pyplot as plt

fig, (ax, ax2) = plt.subplots(ncols=2, sharex=True, sharey=False)
ax.get_shared_x_axes().join(ax, ax2)

ax.plot([1,3,2])
ax2.plot([2,3,1])

plt.show()

enter image description here

Here the ticklabels stay visible. If you don't want that you can turn them off via ax2.set_yticklabels([]).

Community
  • 1
  • 1
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
2

Using plt.ylim() will only adjust the axes for the last figure that was plotted. In order to change the y limits for a specific plot you need to use ax.set_ylim().

So in your case it would be

axes[0].set_ylim(0,25000)
axes[1].set_ylim(0,25000) 
DavidG
  • 24,279
  • 14
  • 89
  • 82