误差线和标记 - 线宽

Errorbars and markers - linewidth

请问如何使markers linewidth=3 和errorbar linewidth=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, 4, 2, 4, 7]
sigma = [0.564, 0.6, 0.8, 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'], df['rv_rvs'], 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='', marker='_', ms=30, linewidth=3, label=rv_teles)
ax.legend(loc='upper left', ncol=1, fontsize=14)
plt.show()

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

times = [1, 2, 3, 4, 5]
rvs = [2, 4, 2, 4, 7]
sigma = [0.564, 0.6, 0.8, 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()

输出: