4

I'm using the "by" option to create facet plots with Pandas like the following:

import pandas as pd
pd.Series(randn(1000)).hist(by=randint(0, 4, 1000), figsize=(6, 4))

enter image description here

Which is great. However I want both the x and y axis to be the same across all plots. Is there a built in way to do this? In ggplot I would use the scales="fixed" option but I don't see something like that built into the Pandas plotting.

Obviously my fallback is to write a few short lines that does this for me, but if there's an automagic way to do this, I'd like to use it.

1 Answer 1

8

You are looking for shared x- and y-axes which can be enabled by passing sharex=True and sharey=True as keyword arguments to hist.

This creates the subplots via matplotlib's subplot machinery. The upside of this as opposed to manually positioning subplots and removing axes is that you can use a large matplotlib toolset to manipulate the axes, e.g.

import matplotlib.pyplot as plt
import pandas as pd
pd.Series(randn(1000)).hist(by=randint(0, 4, 1000), figsize=(6, 4),
                            sharex=True, sharey=True)

ax = plt.gca()
ax.set_xlim(-10, 10)
ax.set_ylim(0, 100)

example with some axis properties set

Sign up to request clarification or add additional context in comments.

1 Comment

as a side note, it appears there's currently (Sept 2014) a bug in Pandas plotting where hist breaks with by if the number of plots does not fully fill a row. For example: pd.Series(randn(1000)).hist(by=randint(0, 5, 1000), sharex=True, sharey=True) I've opened an issue: github.com/pydata/pandas/issues/8256

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.