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))
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With