简体   繁体   English

我该如何要求matplotlib稍微移动重叠曲线,以免它们彼此隐藏?

[英]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)

在此处输入图片说明

You can simply scale the values 您可以简单地缩放值

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

产量

Another option would be to use a dashed line with a darker color that overlaps. 另一种选择是使用虚线,使其具有重叠的较暗颜色。

import matplotlib.pyplot as plt
plt.figure()
plt.plot([1,2,3,4], color="red", label="up", lw=2)
plt.plot([1,2,3,2.5], color="black", linestyle=":", label="down", lw=2)

在此处输入图片说明

暂无
暂无

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

相关问题 如何在 matplotlib 中沿 x 轴稍微移动重叠数据点? - How can I shift overlapping data points slightly along the x-axis in matplotlib? 如何在 matplotlib 中制作抛物线? - How can I make parabolic curves in matplotlib? 如何处理Matplotlib中重叠曲线的“像素化” - How to deal with the “pixelization” of overlapping curves in Matplotlib 我正在使用 Streamlit 构建一个基于答案构建另一个表单的表单,如何隐藏表单以使其不堆叠? - I am using Streamlit to build a form that based on answers builds another form, how do I hide the form so they don't stack? 创建 2 个图,但它们重叠成一个图。 如何使用 matplotlib 创建 2 个单独的图表? - Creating 2 plots but they're overlapping into one graph. How can I create 2 separate graphs using matplotlib? 如何同步到 matplotlib 上的曲线? - How to synchronize to curves on matplotlib? 我如何在没有数学的情况下获得matplotlib中重叠补丁的轮廓 - How can I get the outline of the overlapping patches in matplotlib without maths 如何在循环中为每次迭代同步 colors,在 Matplotlib 中绘制连续曲线? - How can I synch up the colors for each iteration in a loop that plots successive curves in Matplotlib? matplotlib一张图表上的多条线不知道该怎么做? - matplotlib multiple lines on one chart don't get how to do it? 如何偏移x轴刻度线,使彼此之间略低/较高? - How can I offset the x-axis ticks so every other is slightly lower/higher?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM