matplotlib中的误差线显示在其他曲线上 [英] Error bars in matplotlib display over other curves

查看:84
本文介绍了matplotlib中的误差线显示在其他曲线上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图,其中一些数据由带有误差条的散点图表示,我想为其拟合一条曲线.但是,无论我在代码中的哪个位置绘制曲线,误差线都会浮动在其顶部.我希望拟合曲线显示在误差线前面,否则我看不到它.

I have a plot in which some data is represented by a scatter plot with error bars and I want to fit a curve to it. However, no matter where in the code I plot the curve, the error bars float on top of it. I want the fitted curves to display in front of the error bars because otherwise I can't see it.

这是问题的一个简单示例:

Here is a simple example of the issue:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib

x = np.arange(1,10)
r = np.random.random(x.size)

fig1, ax = plt.subplots()
ln1 = ax.plot(2*x,x,'g')
ax3 = ax.twinx()
ln2 = ax3.errorbar(x,r,yerr=x,color='red',fmt='o')
ln2fit = ax3.plot(x,r-0.3,'b')

以及它产生的情节:

有两个轴,因为我要比较两个数据集.

There are two axes because I'm comparing two datasets.

如您所见,即使我在误差线上方绘制了曲线,误差线和点仍然掩盖了曲线.我该怎么做才能禁用它?

As you can see, even though I plotted the curve above the error bars, the error bars and points still obscure the curve. What can I do to disable this?

推荐答案

可以指定zorder:

You can specify the zorder:

ln2 = ax3.errorbar(x,r,yerr=x,color='red',fmt='o',zorder=1)

如果您还希望在前景中显示绿线,则需要将其轴 ax 移至更高的zorder(默认值为0)并隐藏 ax 的轴补丁.code> 以便底层 ax3 保持可见:

If you also want to have the green line in the foreground you need to move it's axes ax to a higher zorder (default is 0) and hide the axes patch of ax so that the then underlying ax3 stays visible:

ax.set_zorder(1)
ax.patch.set_visible(False)

这篇关于matplotlib中的误差线显示在其他曲线上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆