'I cannot plot errors what I exactly want
I want to plot with error bars in both directions. My error values are standard error. So I want the error bars to be according to the value they belong to. Here's my code:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
column_names = ["gplx", "gplxerror", "hplx", "hplxerror"]
data=pd.read_csv("hw4.csv", names=column_names)
x=data.gplx.to_list()
xerr=data.gplxerror.to_list()
y=data.hplx.to_list()
yerr=data.hplxerror.to_list()
xx = [1/(i/1000) for i in x]
yy = [1/(j/1000) for j in y]
plt.errorbar(xx, yy, xerr, yerr, fmt='o',
ecolor='pink', color='blue')
plt.xlabel('Gaia Distance(in pc)')
plt.ylabel('Hipparcos Distance (in pc)')
plt.savefig('filename.png', dpi=600)
And this is the plot that I get:

But the error bars are too big. How can I make them smaller?
Solution 1:[1]
I used (error/100 * value) to get error as percentage of each value. It worked well. Check the following code:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
column_names = ["gplx", "gplxerror", "hplx","hplxerror"]
data=pd.read_csv("hw42.csv", names=column_names)
x=data.gplx.to_list()
xerr=data.gplxerror.to_list()
y=data.hplx.to_list()
yerr=data.hplxerror.to_list()
xx = [1/(i/1000) for i in x]
yy = [1/(j/1000) for j in y]
xxerr = [(i/100) for i in xerr]
yyerr = [(j/100) for j in yerr]
xe= [a * b for a, b in zip(xx, xxerr)]
ye= [a * b for a, b in zip(yy, yyerr)]
plt.errorbar(xx, yy, xe, ye,fmt='.', alpha=1, ecolor='black',elinewidth=0.5, markersize=4)
plt.xlabel('Gaia Distance(in pc)')
plt.ylabel('Hipparcos Distance (in pc)')
plt.savefig('filename.png', dpi=600)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Konstantin Novoselov |
