简体   繁体   English

在使用Pandas绘图时,强制所有子图使用相同的轴范围

[英]Force all subplots to use the same axis range when plotting with Pandas

I'm using the "by" option to create facet plots with Pandas like the following: 我正在使用“by”选项创建带有Pandas的facet图,如下所示:

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. 但是我希望x和y轴在所有图中都相同。 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. 在ggplot中我会使用scales="fixed"选项,但我没有看到像Pandas绘图中内置的那样。

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 . 您正在寻找共享的x轴和y轴,可以通过将sharex=Truesharey=True作为关键字参数传递给hist来启用。

This creates the subplots via matplotlib 's subplot machinery. 这通过matplotlib的子图机制创建子图。 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, eg 与手动定位子图和移除轴相反的优点是,您可以使用大型matplotlib工具集来操纵轴,例如

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)

设置了某些轴属性的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM