5

I have a dataframe like this:

df = pd.DataFrame({'A': [0.3, 0.2, 0.5, 0.2], 'B': [0.1, 0.0, 0.3, 0.1], 'C': [0.2, 0.5, 0.0, 0.7], 'D': [0.6, 0.3, 0.4, 0.6]}, index=list('abcd'))     

    A    B    C    D
a  0.3  0.1  0.2  0.6
b  0.2  0.0  0.5  0.3
c  0.5  0.3  0.0  0.4
d  0.2  0.1  0.7  0.6

Now I want to plot each row as a barplot whereby the y-axis and the x-tick-labels are shared using add_subplot.

Until now, I can only produce a plot that looks like this: enter image description here

There is one problem:

The axes are not shared, how one do this after using add_subplot? Here, this problem is solved by creating one huge subplot; is there any way to do this in a different manner?

My desired outcome looks like the plot above with the only difference, that there are no x-tick-labels in the upper row and now y-tick-labels in the right column.

My current attempt is the following:

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

df = pd.DataFrame({'A': [0.3, 0.2, 0.5, 0.2], 'B': [0.1, 0.0, 0.3, 0.1], 'C': [0.2, 0.5, 0.0, 0.7], 'D': [0.6, 0.3, 0.4, 0.6]}, index=list('abcd'))

fig = plt.figure()
bar_width = 0.35
counter = 1
index = np.arange(df.shape[0])

for indi, rowi in df.iterrows():

    ax = fig.add_subplot(2, 2, counter)
    ax.bar(index, rowi.values, width=bar_width, tick_label=df.columns)
    ax.set_ylim([0., 1.])
    ax.set_title(indi, fontsize=20)
    ax.set_xticks(index + bar_width / 2)
    counter += 1

plt.xticks(index + bar_width / 2, df.columns)
Community
  • 1
  • 1
Cleb
  • 25,102
  • 20
  • 116
  • 151
  • 3
    Do you mean *shared* as in `add_subplot(..., sharex=, sharey=)`? – MB-F Feb 21 '17 at 15:09
  • 2
    @kazemakase: If these flags, `sharex=`, `sharey=`, existed in `add_subplot`, that would indeed be perfect. But I don't see such flags. – Cleb Feb 21 '17 at 15:11
  • 1
    Have you tried? Or are you just saying that they don't exist because you are too lazy? – ImportanceOfBeingErnest Feb 21 '17 at 15:16
  • @Cleb You are right, I can't find this in the documentation either. Still, it works - at least on my machine with Matplotlib 1.5.2. – MB-F Feb 21 '17 at 15:19
  • @ImportanceOfBeingErnest `fig.add_subplot?` returns as potential flags (only the ones starting with s): `sketch_params: unknown snap: unknown` So no, I did not try whether `sharex`, `sharey` exist as they are not listed... – Cleb Feb 21 '17 at 15:21
  • 1
    See: http://matplotlib.org/users/recipes.html#easily-creating-subplots – Nickil Maveli Feb 21 '17 at 15:23
  • 1
    In the documentation of [add_subplot(*args, **kwargs)](http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.add_subplot) it says *kwargs are legal Axes kwargs plus projection* and [`Axes`](http://matplotlib.org/api/axes_api.html#axes-class) does have `sharex=None, sharey=None` as arguments. – ImportanceOfBeingErnest Feb 21 '17 at 15:23
  • https://stackoverflow.com/questions/42973223/how-to-share-x-axes-of-two-subplots-after-they-have-been-created – Martijn Courteaux Apr 17 '23 at 09:18

1 Answers1

2

The question how to produce shared subplots in matplotlib:

What may be more interesting here, is that you could also directly use pandas to create the plot in a single line:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'A': [0.3, 0.2, 0.5, 0.2], 'B': [0.1, 0.0, 0.3, 0.1], 'C': [0.2, 0.5, 0.0, 0.7], 'D': [0.6, 0.3, 0.4, 0.6]}, index=list('abcd'))
df.plot(kind="bar", subplots=True, layout=(2,2), sharey=True, sharex=True)
plt.show()

enter image description here

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