简体   繁体   中英

Re: Setting different color for error bars in matplotlib

I'm currently running with a little bit of trouble with regards to setting a range of colors for my errors bars. Apparently, it looks like there are two error bars imposed to one another. One is color Orange while the other is Red. For reference, I followed the steps from this post: Setting Different error bar colors in bar plot in matplotlib and tweaked to fit in. Is there a way to resolve the small issue?

I'll also include the .csv file for usage . At the moment, the code runs like this:

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

在此处输入图像描述

The loop should enumerate the components individually. The idea is that each error bar is plotted separately, so in each iteration you plot a single combination of x , y , lower , upper , and color :

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)

单独着色的错误栏

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM