[英]How can I ask matplotlib to slightly shift overlapping curves so they don't hide one another?
I am using matplotlib.pyplot
to plot several curves on the same graph. 我正在使用matplotlib.pyplot
在同一张图上绘制几条曲线。 Sometimes they have equal values, so the last one hides the others, like this: 有时它们具有相等的值,因此最后一个隐藏其他值,如下所示:
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()
I would like the curves to be slightly shifted so they don't hide one another, like this: 我希望曲线稍微偏移,这样它们就不会彼此隐藏,就像这样:
Is there any proper way to do this, without changing the actual values I am plotting? 有什么适当的方法可以执行此操作,而不更改我正在绘制的实际值?
Update: The best answer so far (for my case) is that of theImportanceOfBeingErnest . 更新:到目前为止(就我而言)最好的答案是TheImportanceOfBeingErnest 。 However, if I have n curves to plot, instead of just 2, I would have to compute the accumulated offset for each. 但是,如果要绘制n条曲线,而不仅仅是2条曲线,则必须计算每个曲线的累积偏移量。 But given the complex calculations that this answer gets into, I suppose there is no way for matplotlib to do this automatically? 但是考虑到该答案涉及的复杂计算,我想matplotlib无法自动执行此操作吗?
PS: since my values are all multiples of 0.5, the (slight) shifting doesn't risk to create a confusion about the actual value... PS:由于我的值都是0.5的倍数,(轻微)移动不会冒出混淆实际值的风险...
I suppose the usual way of translating an artist with a size in points is to use matplotlib.transforms.offset_copy
. 我想翻译大小为matplotlib.transforms.offset_copy
的艺术家的通常方法是使用matplotlib.transforms.offset_copy
。 Since the default linewidth of lines is 1.5 points, one would translate the curve by approximately that number. 由于默认的线宽为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()
Note that this works well in case the line is spread and without many up- and downs. 注意,这在行扩展且没有很多起伏的情况下效果很好。 A more sophisticated solution (but also largely more complicated) is found in In matplotlib, how can I plot a multi-colored line, like a rainbow 在matplotlib中找到了更复杂的解决方案(但在很大程度上也更复杂) ,我如何绘制多色线,例如彩虹
You can use alpha attribute. 您可以使用alpha属性。
import matplotlib.pyplot as plt
plt.figure()
plt.plot([1,2,3,4], label="up")
plt.plot([1,2,3,2.5], label="down", alpha=.3)
plt.legend()
plt.show()
Alter the ratio between [0, 1] in order to get best way you wanted. 更改[0,1]之间的比率,以获得所需的最佳方式。 With this way you don't have to change your values. 通过这种方式,您不必更改值。
In order to improve visibility you can add a "linewidth" attribute with "alpha". 为了提高可见性,您可以添加带有“ alpha”的“ linewidth”属性。 eg 例如
import matplotlib.pyplot as plt
plt.figure()
plt.plot([1,2,3,4], label="up")
plt.plot([1,2,3,2.5], label="down", linewidth=4, alpha=.5)
plt.legend()
plt.show()
Change the values as you like. 根据需要更改值。
I would use a transform
that moves the points in the second plot downward slightly (you can also apply it to the first plot to move the points upward): 我将使用一种transform
,将第二个绘图中的点稍微向下移动(也可以将其应用于第一个绘图中以将点向上移动):
import matplotlib.pyplot as plt
from matplotlib import transforms
fig, ax = plt.subplots()
transform = transforms.Affine2D().translate(0, -0.03) + ax.transData
ax.plot([1, 2, 3, 4], label="up")
ax.plot([1, 2, 3, 2.5], label="down", transform=transform)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.