【问题标题】:Re: Setting different color for error bars in matplotlib回复:在 matplotlib 中为误差线设置不同的颜色
【发布时间】:2021-07-15 05:11:46
【问题描述】:

我目前在为我的错误栏设置颜色范围方面遇到了一些麻烦。显然,看起来有两个错误栏相互强加。一种是橙色,另一种是红色。作为参考,我按照这篇文章中的步骤操作:Setting Different error bar colors in bar plot in matplotlib 并进行了调整以适应。有没有办法解决这个小问题?

我还将包括.csv file for usage。目前,代码运行如下:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy.stats import linregress

df = pd.read_csv('Regression.csv') 
df.head()

A = df['Period'] #Period in Days 
B = df['Luminosity'] #Luminosity is already Logarithmic prior to calculation
colors = ['red', 'orange', 'forestgreen', 'teal', 'navy', 'darkorchid'] #range of colors

#Luminosity Uncertainties
ylower_error = df['-dY lum'] #lower limit
yupper_error = df['+dY lum'] #upper limit
yasymmetric_error = [ylower_error, yupper_error]

#Regression space
slope, intercept, r_value, p_value, std_err = linregress(np.log10(A+1), np.array(B))

xfid = np.linspace(2,3)   # This is just a set of x to plot the straight line 

#Plotting the overall data
fig, ax = plt.subplots(figsize=(12, 10))

ax.scatter(np.log10(A), np.array(B), marker='*', s=300, color=colors)
ax.plot(xfid, xfid*slope+intercept,  '-.', c='royalblue', linewidth=3, zorder=0)
ax.set_xlim([2.46, 2.77]) #X-Limits
ax.set_ylim([4.35, 5.45]) #Y-Limits

#Setting the range of colors for the error bar. This one came from the reference post.
for yerr, color in zip(yasymmetric_error, colors):
    ax.errorbar(np.log10(A), np.array(B), yerr, lw=2, capsize=3, 
                capthick=2, ls='none', ecolor=color)

plt.rcParams['axes.linewidth'] = 4
plt.tight_layout()

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    循环应该单独枚举组件。这个想法是每个误差条都是单独绘制的,因此在每次迭代中,您绘制xyloweruppercolor 的单个组合:

    for x, y, lower, upper, color in zip(np.log10(A), np.array(B), ylower_error, yupper_error, colors):
        ax.errorbar(x, y, yerr=np.array([[lower], [upper]]), lw=2, capsize=3, 
                    capthick=2, ls='none', ecolor=color)
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 2014-03-21
      • 2013-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 1970-01-01
      相关资源
      最近更新 更多