使用误差条matplotlib制作椭圆标记(making ellipse markers with error bars matplotlib)

编程入门 行业动态 更新时间:2024-10-28 21:23:33
使用误差条matplotlib制作椭圆标记(making ellipse markers with error bars matplotlib)

我需要制作一个带有省略号作为标记的图(带有错误栏)。 经过一番搜索后,我在matplotlib.patches提出了Ellipse 。 然后我可以用plt.errorbar绘制误差条。 但问题在于,即使我先给出错误栏命令,错误条总是在前景中绘制,而省略号在背景上绘制,无论我在程序中给出什么顺序。

有没有人知道一个更好的方法来创建一个椭圆作为标记(每个点将有不同的偏心率)与误差条? 或者至少指导我如何将错误栏放在后台?

这是我到目前为止的最小例子:

import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages from matplotlib.patches import Ellipse PlotFileName="test.pdf" pdf = PdfPages(PlotFileName) fig=plt.figure(1) ax1=fig.add_subplot(111) plt.xlim([1,4]) plt.ylim([2,8]) ax1.errorbar([2.5], [5], yerr=[1], fmt="o", color="black", ms=0.1) ax1.add_artist(Ellipse((2.5, 5), 1, 1, facecolor="green", edgecolor="black")) pdf.savefig(fig) pdf.close() plt.close()

以下是它的外观:

我希望错误栏进入椭圆的背景。

提前致谢...

I need to make a plot (with errorbars) with ellipses as markers. After some searching I came up with Ellipse in matplotlib.patches. Then I could draw the error bars with plt.errorbar. But the problem is that even though I give the error bar command first, the error bars are always drawn in the foreground and the ellipses are drawn on the background, no matter what order I give in the program.

Does any one know of a better way to create an ellipse as a marker (each point will have a different eccentricity) with error bars? Or at least guide me in how to put the error bars in the background?

Here is a minimal example of what I have so far:

import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages from matplotlib.patches import Ellipse PlotFileName="test.pdf" pdf = PdfPages(PlotFileName) fig=plt.figure(1) ax1=fig.add_subplot(111) plt.xlim([1,4]) plt.ylim([2,8]) ax1.errorbar([2.5], [5], yerr=[1], fmt="o", color="black", ms=0.1) ax1.add_artist(Ellipse((2.5, 5), 1, 1, facecolor="green", edgecolor="black")) pdf.savefig(fig) pdf.close() plt.close()

and here is how it looks:

I want the error bar to go in the background of the ellipse.

Thanks in advance...

最满意答案

对绘图命令使用zorder说明符。 从文档中:“为艺术家设置zorder。首先绘制具有较低zorder值的艺术家。”

import matplotlib.pyplot as plt from matplotlib.patches import Ellipse fig=plt.figure(1) ax1=fig.add_subplot(111) plt.xlim([0,5]) plt.ylim([0,10]) ax1.errorbar([2.5], [5], yerr=[1], fmt="o", color="black", ms=0.1, zorder=1) ax1.add_artist(Ellipse((2.5, 5), 1, 1, facecolor="green", edgecolor="black",zorder=2)) plt.show() exit(0)

Use the zorder specifier for both your plot commands. From the documentation: "Set the zorder for the artist. Artists with lower zorder values are drawn first."

import matplotlib.pyplot as plt from matplotlib.patches import Ellipse fig=plt.figure(1) ax1=fig.add_subplot(111) plt.xlim([0,5]) plt.ylim([0,10]) ax1.errorbar([2.5], [5], yerr=[1], fmt="o", color="black", ms=0.1, zorder=1) ax1.add_artist(Ellipse((2.5, 5), 1, 1, facecolor="green", edgecolor="black",zorder=2)) plt.show() exit(0)

更多推荐

本文发布于:2023-07-29 14:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316829.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:误差   椭圆   标记   matplotlib   making

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!