gpt4 book ai didi

python - Matplotlib 添加一个特定的刻度表示轴最大值 - 多尺度单次观察

转载 作者:太空狗 更新时间:2023-10-30 01:16:13 24 4
gpt4 key购买 nike

尝试将观察结果分别绘制到每个观察的多个尺度上,我设法生成了以下图:

enter image description here

不过,我想在每个刻度中添加一个显示 y-max 值的刻度,而不考虑它与前一个刻度之间的差距。此类图的示例如下所示。它在 y-max 是滴答间隔的倍数时产生。

enter image description here

谢谢,F.

这是用于生成这些示例的代码。

import numpy as np
import pylab as pl
import matplotlib as plt
import matplotlib.ticker as ticker
import matplotlib.transforms

def add_scales(fig, axes, scales, subplot_reduction_factor=0.1, margin_size=50):
nb_scales = len(scales)
b,l,w,h = zoom_ax.get_position().bounds

_, ymax = axes.get_ylim()

# Saves some space to the right so that we can add our scales
fig.subplots_adjust(right=1-(subplot_reduction_factor)*nb_scales)

for (n, (vmin, vmax, color, label, alignment)) in enumerate(scales):

# Adjust wrt. the orignial figure's scale
nax = fig_zoom.add_axes((b,l,w,(h * alignment) / ymax))
nax.spines['right'].set_position(('outward', -40+n*margin_size))
nax.set_ylim((vmin,vmax))

# Move ticks and label to the right
nax.yaxis.set_label_position('right')
nax.yaxis.set_ticks_position('right')

# Hides everything except yaxis
nax.patch.set_visible(False)
nax.xaxis.set_visible(False)
nax.yaxis.set_visible(True)
nax.spines["top"].set_visible(False)
nax.spines["bottom"].set_visible(False)

# Color stuff
nax.spines['right'].set_color(color)
nax.tick_params(axis='y', colors=color)
nax.yaxis.set_smart_bounds(False)
#nax.yaxis.label.set_color(color)

if label != None:
nax.set_ylabel(None)

if __name__ == '__main__':

a=(np.random.normal(10,5,100))

a=np.linspace(0,100,100)
c=np.linspace(0,80, 100)
d=np.linspace(0,40,100)


fig_zoom = plt.pyplot.figure()
zoom_ax = fig_zoom.add_subplot(1,1,1)


zoom_ax.plot(a,c)
zoom_ax.plot(a,d)
zoom_ax.set_title('Zoom')
zoom_ax.set_xlabel('A')
zoom_ax.set_ylabel('B')
zoom_ax.set_ylim((0,100))
zoom_ax.grid()
add_scales(fig_zoom,
zoom_ax, [(0,.55,'green',None,40),
(0,.85,'blue',None,80)])

fig_zoom.savefig(open('./test.svg','w'),format='svg')

最佳答案

您可以将最高 ytick 值设置为最大值。如果第二高的 ytick 值和您的最大值非常接近,标签可能会困惑。

尝试将此添加到您的循环中:

tcks = nax.get_yticks()
tcks[-1] = vmax
nax.set_yticks(tcks)

enter image description here

关于python - Matplotlib 添加一个特定的刻度表示轴最大值 - 多尺度单次观察,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15291032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com