【问题标题】:Errorbars and markers - linewidth误差线和标记 - 线宽
【发布时间】:2021-06-26 12:41:22
【问题描述】:

如何使标记 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, 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()

【问题讨论】:

    标签: python-3.x matplotlib errorbar


    【解决方案1】:

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

    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()
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-14
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      • 2012-02-16
      相关资源
      最近更新 更多