Matplotlib-如何为一系列绘图设置ylim()? [英] Matplotlib - How do I set ylim() for a series of plots?
问题描述
我要制作一系列箱形图,每个箱形图的范围都不同。我尝试通过确定每个单独系列的最大值和最小值来设置ylim。但是,在许多情况下,最小值是一个离群值,因此该图被压缩了。如何选择情节胡须使用的相同限制(加上少量保证金)?
I have a series of box plots I am trying to make, each of which has a different range. I tried setting ylim by determining the max and min of each separate series. However, the min in many cases is an outlier, and so the plot is compressed. How can I select the same limit used by the 'whiskers' of the plot (plus a small margin)?
例如,现在我正在这样做:
Eg, right now I'm doing this:
[In]
ax = df['feature'].boxplot()
ymax = max(df['feature']
ymin = min(df['feature']
ax.set_ylim([ymax,ymin])
我想将ymax设置为
推荐答案
作为@unutbu建议的替代方案,您可以避免绘制离群值,然后使用 ax.margins(y = 0)
(或一些小的 eps
)可将限制范围扩展到晶须的范围。
As an alternative to what @unutbu suggested, you could avoid plotting the outliers and then use ax.margins(y=0)
(or some small eps
) to scale the limits to the range of the whiskers.
例如:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.poisson(5, size=(100, 5)))
fig, ax = plt.subplots()
#Note showfliers=False is more readable, but requires a recent version iirc
box = df.boxplot(ax=ax, sym='')
ax.margins(y=0)
plt.show()
如果您想在最大的晶须周围留一点空间,请使用 ax.margins(0.05 )
添加范围的5%而不是范围的0%:
And if you'd like a bit of room around the largest "whiskers", use ax.margins(0.05)
to add 5% of the range instead of 0% of the range:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.poisson(5, size=(100, 5)))
fig, ax = plt.subplots()
box = df.boxplot(ax=ax, sym='')
ax.margins(y=0.05)
plt.show()
这篇关于Matplotlib-如何为一系列绘图设置ylim()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!