微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

误差线和标记 - 线宽

如何解决误差线和标记 - 线宽

如何像图例中那样制作标记 linewidth=3 和 errorbars linewith=1?错误栏中的 linewidth=1 不起作用。非常感谢

import matplotlib.pyplot as plt
from matplotlib.lines import Line2D  # for legend handle
import pandas as pd
import numpy as np

times = [1,2,3,4,5]
rvs = [2,7]
sigma = [0.564,0.6,0.8,0.4]
rv_telescopes = ['A','B','A','C','C']

d = {'rv_times': times,'rv_rvs': rvs,'rv_sigma': sigma,'rv_telescopes': rv_telescopes}
df = pd.DataFrame(data=d)
colors = {'A': '#008f00','B': '#e36500','C': 'red'}

fig,ax = plt.subplots(figsize=(10,10))
ax.errorbar(df['rv_times'],df['rv_rvs'],df['rv_sigma'],color='none',ecolor=df['rv_telescopes'].map(colors),linewidth=1)
ax.scatter(df['rv_times'],marker='_',linewidth=3,color=df['rv_telescopes'].map(colors),s=1000)

for rv_teles in np.unique(df['rv_telescopes']):
     color = colors[rv_teles]
     df1 = df[df['rv_telescopes'] == rv_teles]  # filter out rows corresponding to df['rv_telescopes']
     ax.errorbar(df1['rv_times'],df1['rv_rvs'],df1['rv_sigma'],color=color,ls='',ms=30,label=rv_teles)
ax.legend(loc='upper left',ncol=1,fontsize=14)
plt.show()

enter image description here

解决方法

我的猜测是,您可能已经使用不同的线宽值绘制了两次东西。这可以通过删除第一次绘制的代码部分来解决:

times = [1,2,3,4,5]
rvs = [2,7]
sigma = [0.564,0.6,0.8,0.4]
rv_telescopes = ['A','B','A','C','C']

d = {'rv_times': times,'rv_rvs': rvs,'rv_sigma': sigma,'rv_telescopes': rv_telescopes}
df = pd.DataFrame(data=d)
colors = {'A': '#008f00','B': '#e36500','C': 'red'}

fig,ax = plt.subplots(figsize=(10,10))

for rv_teles in np.unique(df['rv_telescopes']):
     color = colors[rv_teles]
     df1 = df[df['rv_telescopes'] == rv_teles]
     ax.errorbar(df1['rv_times'],df1['rv_rvs'],df1['rv_sigma'],color=color,ls='',marker='_',ms=30,linewidth=3,label=rv_teles)
ax.legend(loc='upper left',ncol=1,fontsize=14)
plt.show()

输出:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。