使用错误条matplotlib制作椭圆标记

时间:2012-07-04 04:49:27

标签: python matplotlib

我需要制作一个带有省略号作为标记的图表(带有错误栏)。经过一番搜索,我在Ellipse中找到了matplotlib.patches。然后我可以用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()

以下是它的外观: error bar infront of ellipse

我希望错误栏位于椭圆的背景中。

提前致谢...

2 个答案:

答案 0 :(得分:2)

对两个绘图命令使用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)

答案 1 :(得分:0)

Working with Path在我看来,使用Path是一种更直接的方法:Path实例被完全视为普通的marker,因此只使用完全相同的接口。请查看下面的示例,但也请参考此主题的matplotlib documentation

import numpy as np

import matplotlib.pyplot as plt 
import matplotlib.path as mpath

# Create mock data.
theta = np.linspace(0, 2.*np.pi, 30) 
signa = np.sin(theta)

u_theta = np.random.normal(0., scale=0.15, size=signa.size)
u_signa = np.random.normal(0., scale=0.15, size=signa.size)

theta += u_theta
signa += u_signa

# Define the ellipse marker.
circle = mpath.Path.unit_circle()
verts = np.copy(circle.vertices)
verts[:, 0] *= 1.618
ellipt_marker = mpath.Path(verts, circle.codes)

# Done, basically.[![Plotting example][1]][1]
plt.errorbar(theta, signa, xerr=u_theta, yerr=u_signa,
             marker=ellipt_marker, linestyle='', capsize=5,
             ms=20, mfc='w', c='r', mec='g')

plt.xlabel('Autology', fontsize=35)
plt.ylabel('Eterology', fontsize=35)
plt.show()