Exa
Exa

Reputation: 468

Histograms side by side: change axis

I have the following two dummy dfs:

# First df
columns_One = ['count']
data_One = [[1],
       [100],
       [100],
       [100],
        [120],
        [500],
        [540],
        [590]]
dummy_One = pd.DataFrame(columns=columns_One, data=data_One)

# Second df
columns_Two = ['count']
data_Two = [[1],
       [100],
       [102],
        [300],
        [490],
        [400],
        [400],
        [290]]
dummy_Two = pd.DataFrame(columns=columns_Two, data=data_Two)

I want to plot them as a histogram. It currently looks like this:

enter image description here

How I plot it:

fig, axes = pyplot.subplots(1,2)


dummy_One.hist('count', ax=axes[0])
dummy_Two.hist('count', ax=axes[1])

pyplot.show()

I am struggling with the following:

  1. How can I change the y with the x axis?
  2. How can I set an universal y label and x label for both charts?
  3. How can I set the range for the y and x axis for both charts?

Upvotes: 0

Views: 594

Answers (1)

D. Rooij
D. Rooij

Reputation: 94

  1. I assume that you simply want to rotate the plot, add orientation="horizontal".

  2. You can share the axis of subplots by setting sharex=True or sharey=True.

  3. To share the range simply specify the limits of either axis: plt.xlim(0,4) and plt.ylim(-100,700)

import matplotlib.pyplot as plt
fig, axes = plt.subplots(1,2, sharex=True, sharey=True)


dummy_One.hist(ax=axes[0], orientation="horizontal")
dummy_Two.hist(ax=axes[1], orientation="horizontal")

plt.ylim(-100,700)
plt.xlim(0,4)
plt.show()

enter image description here

Upvotes: 2

Related Questions