我需要制作一个带有省略号作为标记的图(带有错误栏)。 经过一番搜索后,我在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)更多推荐
发布评论