限制几个轴中的一个轴的范围。

huangapple go评论82阅读模式
英文:

How to limit the range of only one axis out of several

问题

这段代码的目的是创建一个图表,其中包含两个y轴,其中一个轴上绘制了紫色的图形。如果您想限制紫色图形的范围,可以使用ax1.set_ylim()来设置ax1轴的y坐标范围,以便只显示您感兴趣的部分。例如,如果您希望范围在-100到-50之间,可以这样设置:

ax1.set_ylim(-100, -50)

这将使紫色图形仅显示在-100到-50的范围内。

英文:

Here's the code:

fig, ax1 = plt.subplots(figsize=(20, 10))

pds, ns, ss, avgs = polar
ax1.plot(pds, avgs, color="purple", linestyle="-")
ax1.tick_params(axis='y', labelcolor="k")
ax1.set_ylabel("polar field strength")

ax2 = ax1.twinx()

for i, (cmd, ffmd, md, c) in enumerate(zip(cmds, ffmds, mds, cycles)):
    p = ax2.plot(c[0], c[1], label="SC" + str(25 - len(cycles) + i + 1))
    color = p[-1].get_color()
    ax2.axvline(x=cmd, color=color, linestyle=":")
    ax2.axvline(x=md, color=color, linestyle = '--')
    ax2.axvline(x=ffmd, color=color, linestyle = '-')

sc25 = cycles[-1]
p = ax2.plot(sc25[0][:-6], sc25[1][:-6], label="SC25")
color = p[-1].get_color()
ax2.axvline(x=cmds[-1], color=color, linestyle=":")
ax2.tick_params(axis='y', labelcolor="k")
ax2.set_ylabel("SSN")

This works as intended, producing this plot:

限制几个轴中的一个轴的范围。

However, I would much prefer it if the purple plot which belongs to ax1 were limited to only part of the axis, so as to not clutter up the plot needlessly. In this case it would for example be great to limit the entire range to just the portion of the axis that is now covered by -100 to -50, placing the entire range of the purple plot there. Any suggestions on how to do this?

答案1

得分: 1

在ax1中,紫色图是唯一的,您可以调整y轴限制,以将紫色图放在您想要的位置。例如:

ax1.set_ylim((-300, 500))

您还可以进一步限制y轴刻度范围。例如:

ax1.set_yticks([-200, -100, 0, 100, 200])
英文:

Given that the purple plot is the only one in ax1, you can play with the y limits, in order to place your purple plot in the position you want. For instance:

ax1.set_ylim((-300, 500))

You can go further, and limit the y ticks to the relevant range. For instance:

ax1.set_yticks([-200, -100, 0, 100, 200])

huangapple
  • 本文由 发表于 2023年5月13日 21:35:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定