隐藏Matplotlib中空心点下的误差条。

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

Hide errobar under hollow points in Matplotlib

问题

I want to plot data with errors. The data points are hollow diamonds, and I do not want the part of the error showing inside the hollow diamonds. I just want it to show on the outside of the diamonds, leaving the inside empty. The code part I use is:

plt.errorbar(x=unknown['asd'], y=(unknown['dsa'])*-10, 
xerr=unknown['asderr'], yerr=(unknown['dsaerr']*-10), linestyle="None", fmt='D', markersize=7, 
ecolor='b', elinewidth=0.2, capsize=0.05, markerfacecolor='white', markeredgecolor='blue',
label=None, alpha=0.6)

I tried to use zorder:

plt.errorbar(x=unknown['asd'], y=(unknown['dsa'])*-10, 
xerr=unknown['asderr'], yerr=(unknown['dsaerr']*-10), linestyle="None", fmt='D', markersize=7, 
ecolor='b', elinewidth=0.2, capsize=0.05, markerfacecolor='white', markeredgecolor='blue',
label=None, alpha=0.6, zorder=1)

plt.plot(unknown['asd'], (unknown['dsa'])*-10, marker='D', markerfacecolor='none', markeredgecolor='blue', 
markeredgewidth=1, markersize=7, linestyle='None', zorder=2, alpha=0.6)

But I still see the errorbars passing through the hollow diamonds. Is there any quick way to make the errorbar line not shown under the hollow parts?

隐藏Matplotlib中空心点下的误差条。

英文:

I want to plot data with errors. The data points are hollow diamonds and I do not want the part of the error showing inside the hollow diamonds. I just want it to show on outside the diamonds, leaving the inside empty. The code part I use is:

plt.errorbar(x=unknown['asd'], y=(unknown['dsa'])*-10, 
xerr=unknown['asderr'], yerr=(unknown['dsaerr']*-10), linestyle="None", fmt='D', markersize=7, 
ecolor='b', elinewidth=0.2, capsize=0.05, markerfacecolor='white', markeredgecolor='blue',
label=None, alpha=0.6)

I tried to use zorder :

plt.errorbar(x=unknown['asd'], y=(unknown['dsa'])*-10, 
xerr=unknown['asderr'], yerr=(unknown['dsaerr']*-10), linestyle="None", fmt='D', markersize=7, 
ecolor='b', elinewidth=0.2, capsize=0.05, markerfacecolor='white', markeredgecolor='blue',
label=None, alpha=0.6, zorder=1)

plt.plot(unknown['asd'], (unknown['dsa'])*-10, marker='D', markerfacecolor='none', markeredgecolor='blue', 
markeredgewidth=1, markersize=7, linestyle='None', zorder=2, alpha=0.6)

but I still see the errorbars passing through the hollow diamonds. Is there any quick way to make the errorbar line not shown under the hollow parts?

隐藏Matplotlib中空心点下的误差条。

答案1

得分: 1

以下是翻译好的部分:

这是由于plt.errorbar调用中设置的alpha值引起的。

以下是你正在做的更简单的示例:

import matplotlib.pyplot as plt

markers_style = dict(fmt='D', markersize=20, markerfacecolor='white', markeredgecolor='blue')
lines_style = dict(ecolor='b', elinewidth=1, capsize=0.05)

plt.errorbar(x=10, y=10, xerr=0.5, yerr=0.5, **markers_style, **lines_style, alpha=0.6)

移除alpha=0.6,你将获得以下图形:

为某种原因,你可能想要保留errorbar调用中的alpha值。在这种情况下,为了实现所需的图形,你将不得不创建第二个绘图(plt.plotplt.scatter)来“隐藏”每个中空菱形的中心。

# [...]
plt.errorbar(x=10, y=10, xerr=0.5, yerr=0.5, **markers_style, **lines_style, alpha=0.6)

plt.plot(10, 10, marker='D', c="w", markersize=20)

希望对你有所帮助。

英文:

This is due to the alpha value set in the plt.errorbar call.

Here is a simpler example of what you're doing:

import matplotlib.pyplot as plt

markers_style = dict(fmt='D', markersize=20, markerfacecolor='white', markeredgecolor='blue')
lines_style = dict(ecolor='b', elinewidth=1, capsize=0.05)

plt.errorbar(x=10, y=10, xerr=0.5, yerr=0.5, **markers_style, **lines_style, alpha=0.6)

隐藏Matplotlib中空心点下的误差条。

Removing alpha=0.6, you'll obtain the following figure

隐藏Matplotlib中空心点下的误差条。

For some reason, you might want to keep the alpha value in the errobar call. In this case, to achieve the desired figure you'll have to create a second plot (plt.plot or plt.scatter) to hide the center of each hollow diamond.

# [...]
plt.errorbar(x=10, y=10, xerr=0.5, yerr=0.5, **markers_style, **lines_style, alpha=0.6)

plt.plot(10, 10, marker='D', c="w", markersize=20)

隐藏Matplotlib中空心点下的误差条。

huangapple
  • 本文由 发表于 2023年4月13日 20:31:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005477.html
匿名

发表评论

匿名网友

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

确定