[英]Adding error bars to a graph with file data using matplotlib
我有一個名為J.txt的文件,該文件包含3列數據,第三個是錯誤欄(對稱)的值,如下所示:
2454501.81752 0.0018087812 9.69699358080375E-005
2454537.77104 0.0030887732 0.0001610068
2454603.70872 0.0022500182 0.0001230047
2454640.56455 0.0013261298 7.57575739971879E-005
2454662.63581 0.0017888998 9.91743434734387E-005
而且我有以下代碼:
import numpy as np
import matplotlib.pyplot as plt
plt.plotfile('J.txt', delimiter=' ', cols=(0, 1),
names=('V', 'V-B'), marker='o', markersize=2, markeredgewidth=0.1, markeredgecolor='black',
color= 'green', label= "Cluster", linestyle='None', newfig=False)
plt.show()
我一直試圖將誤差線添加到繪圖中,因為我不知道如何將文件中的列與plt.errorbar
(如果那是我應該使用的)。
干杯。
根據Will和roadrunner66的建議,我挖掘了一個類似的解決方案:
import numpy as np
import matplotlib.pyplot as plt
col0 = np.genfromtxt('J.txt', usecols=(0), delimiter=' ', dtype=None)
col1 = np.genfromtxt('J.txt', usecols=(1), delimiter=' ', dtype=None)
col2 = np.genfromtxt('J.txt', usecols=(2), delimiter=' ', dtype=None)
fig, ax1 = plt.subplots()
ax1.plot(col0, col1, c='b', marker='o', markeredgewidth=0, linewidth=0, markersize=3)
ax1.errorbar(col0, col1, yerr=col2, linestyle=' ', c= 'b')
plt.show()
Numpy的np.genfromtxt
將所需的數據帶入,而matplotlib的ax1.errorbar
使我可以使用Y軸的yerr
值放置誤差線。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.