简体   繁体   English

回复:为 matplotlib 中的误差线设置不同的颜色

[英]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.我目前在为我的错误栏设置 colors 范围方面遇到了一些麻烦。 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?作为参考,我按照这篇文章中的步骤进行操作: Setting Different error bar colors in bar plot in matplotlib并调整以适应。有没有办法解决这个小问题

I'll also include the .csv file for usage .我还将包括.csv 文件以供使用 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 :这个想法是每个误差条都是单独绘制的,因此在每次迭代中,您 plot 是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)

单独着色的错误栏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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