英文:
How to prevent pyplot.errorbar from shifting x-axis of seaborn barplot
问题
我想使用Seaborn的barplot绘制数据;我只有均值和标准差。我使用pyplot.errorbar将误差条添加到我的图中,但是它使我的x轴稍微偏移(请参见图中的红色星号)。如何防止这种情况发生?
复制代码:
import seaborn as sn
import matplotlib.pyplot as plt
### 加载示例数据 ###
health = sns.load_dataset('healthexp')
health_summary = health.groupby(['Country']).Life_Expectancy.agg({'mean','std'}).reset_index()
### 不带误差条的barplot ###
p = sn.barplot(health_summary, x = 'Country', y = 'mean', errorbar=None)
plt.show()
### 带误差条的barplot ###
p = sn.barplot(health_summary, x = 'Country', y = 'mean', errorbar=None)
p.errorbar(x=health_summary['Country'], y=health_summary['mean'], yerr=health_summary['std'], fmt="none", c="k")
plt.show()
英文:
I want to plot data using Seaborn barplot; I only have the mean and standard deviation. I use pyplot.errorbar to add error bars to my plot, however, it shifts my x axis slightly (see red star below in plot). How do I prevent this from happening?
Code to reproduce:
import seaborn as sn
import matplotlib.pyplot as plt
### loading example data ###
health = sns.load_dataset('healthexp')
health_summary = health.groupby(['Country']).Life_Expectancy.agg({'mean','std'}).reset_index()
### barplot without errorbars ###
p = sn.barplot(health_summary, x = 'Country', y = 'mean', errorbar=None)
plt.show()
### barplot with errorbars ###
p = sn.barplot(health_summary, x = 'Country', y = 'mean', errorbar=None)
p.errorbar(x=health_summary['Country'], y=health_summary['mean'], yerr=health_summary['std'], fmt="none", c="k")
plt.show()
答案1
得分: 2
在调用errorbar
之前,您需要保存xlim
,然后恢复其值。
您可以使用上下文管理器方便地实现:
from contextlib import contextmanager
import matplotlib.pyplot as plt
import seaborn as sns
@contextmanager
def fixed_xlim(ax):
xlims = ax.get_xlim()
try:
yield
finally:
ax.set_xlim(xlims)
health = sns.load_dataset("healthexp")
health_summary = (
health.groupby(["Country"]).Life_Expectancy.agg({"mean", "std"}).reset_index()
)
fig, (ax1, ax2) = plt.subplots(nrows=2)
p = sns.barplot(health_summary, x="Country", y="mean", errorbar=None, ax=ax1)
p = sns.barplot(health_summary, x="Country", y="mean", errorbar=None, ax=ax2)
with fixed_xlim(ax2):
p.errorbar(
x=health_summary["Country"],
y=health_summary["mean"],
yerr=health_summary["std"],
fmt="none",
c="k",
)
plt.tight_layout()
plt.show()
英文:
You need to save the xlim
before calling errorbar
and then restore its values.
You can make it handy with a context manager:
from contextlib import contextmanager
import matplotlib.pyplot as plt
import seaborn as sns
@contextmanager
def fixed_xlim(ax):
xlims = ax.get_xlim()
try:
yield
finally:
ax.set_xlim(xlims)
health = sns.load_dataset("healthexp")
health_summary = (
health.groupby(["Country"]).Life_Expectancy.agg({"mean", "std"}).reset_index()
)
fig, (ax1, ax2) = plt.subplots(nrows=2)
p = sns.barplot(health_summary, x="Country", y="mean", errorbar=None, ax=ax1)
p = sns.barplot(health_summary, x="Country", y="mean", errorbar=None, ax=ax2)
with fixed_xlim(ax2):
p.errorbar(
x=health_summary["Country"],
y=health_summary["mean"],
yerr=health_summary["std"],
fmt="none",
c="k",
)
plt.tight_layout()
plt.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论