简体   繁体   English

在 matplotlib 中设置轴限制但在其中自动缩放

[英]Set axis limits in matplotlib but autoscale within them

Is it possible to set the max and min values of an axis in matplotlib, but then autoscale when the values are much smaller than these limits?是否可以在 matplotlib 中设置轴的最大值和最小值,然后在值远小于这些限制时自动缩放?

For example, I want a graph of percentage change to be limited between -100 and 100, but many of my plots will be between, say, -5 and 5. When I use ax.set_ylim(-100, 100) , this graph is very unclear.例如,我希望将百分比变化图限制在 -100 和 100 之间,但我的许多图将介于 -5 和 5 之间。当我使用ax.set_ylim(-100, 100) ,此图很不清楚。

I suppose I could use something like ax.set_ylim(max((-100, data-n)), min((100, data+n))) , but is there a more built in way to achieve this?我想我可以使用ax.set_ylim(max((-100, data-n)), min((100, data+n))) ,但是有没有更内置的方法来实现这一点?

If you want to drop extreme outliers you could use the numpy quantile function to find say the first 0.001 % of the data and last 99.999 %.如果您想删除极端异常值,您可以使用 numpy quantile函数来查找数据的前 0.001% 和最后 99.999%。

near_min = np.quantile(in_data, 0.0001)
near_max = np.quantile(in_data, 0.9999)
ax.set_ylim(near_min, near_max)

You will need to adjust the quantile depending on the volume of data you drop.您需要根据丢弃的数据量调整分位数。 You might want to include some test of whether the difference between near_min and true min is significant?您可能想要包含一些关于 Near_min 和 true min 之间的差异是否显着的测试?

As ImportanceOfBeingErnest pointed out , there is no support for this feature.正如 ImportanceOfBeingErnest 指出的那样,不支持此功能。 In the end I just used my original idea, but scaled it by the value of the max and min to give the impression of autoscale.最后我只是使用了我最初的想法,但是通过 max 和 min 的值对其进行了缩放,以给出自动缩放的印象。

ax.set_ylim(max((-100, data_min+data_min*0.1)), min((100, data_max+data_max*0.1)))

Where for my case it is true that对于我的情况,哪里是真的

data_min <= 0, data_max >= 0

Why not just set axis limits based on range of the data each time plot is updated?为什么不只是在每次更新绘图时根据数据范围设置轴限制?

ax.set_ylim(min(data), max(data))

Or check if range of data is below some threshold, and then set axis limits.或者检查数据范围是否低于某个阈值,然后设置轴限制。

if min(abs(data)) < thresh:
    ax.set_ylim(min(data), max(data))

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

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