gpt4 book ai didi

python - 我怎样才能让 matplotlib 稍微移动重叠的曲线,这样它们就不会互相隐藏?

转载 作者:行者123 更新时间:2023-11-28 20:55:53 26 4
gpt4 key购买 nike

我正在使用 matplotlib.pyplot 在同一张图上绘制多条曲线。有时它们具有相同的值,因此最后一个隐藏了其他值,如下所示:

import matplotlib.pyplot as plt
plt.figure()
plt.plot([1,2,3,4], label="up")
plt.plot([1,2,3,2.5], label="down")
plt.show()

hidden curve

我希望曲线稍微移动,这样它们就不会互相隐藏,就像这样:

all curves visible

有没有什么合适的方法可以做到这一点,而不改变我正在绘制的实际值?

更新:到目前为止(对我来说)最好的答案是 that of theImportanceOfBeingErnest .但是,如果我要绘制 n 条曲线,而不是只有 2 条,我将不得不计算每条曲线的累积偏移量。但考虑到 this answer 的复杂计算进入,我想 matplotlib 没有办法自动执行此操作?

PS:因为我的值都是 0.5 的倍数,所以(轻微的)偏移不会造成对实际值造成混淆的风险......

最佳答案

我想翻译一个以点为单位的艺术家的通常方法是使用 matplotlib.transforms.offset_copy .由于线的默认线宽为 1.5 磅,因此可以将曲线平移大约该数字。

import matplotlib.pyplot as plt
import matplotlib.transforms as mtrans

fig, ax = plt.subplots()
ax.plot([1,2,3,4], label="up")

tr = mtrans.offset_copy(ax.transData, fig=fig, x=0.0, y=-1.5, units='points')
ax.plot([1,2,3,2.5], label="down", transform=tr)

plt.show()

enter image description here

请注意,这在线条较宽且没有太多起伏的情况下效果很好。在 In matplotlib, how can I plot a multi-colored line, like a rainbow 中可以找到更复杂的解决方案(但也更复杂)

关于python - 我怎样才能让 matplotlib 稍微移动重叠的曲线,这样它们就不会互相隐藏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55710343/

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