Matplotlib 散点图 Errorbar 偏离位置
Matplotlib Scatter plot Errorbar way off position
我正在尝试创建一个带有不对称误差线的散点图,但误差线的位置偏离了。它应该只是 x_err=[[low_value_error], [max_value_error]] 但我似乎无法让它工作。我尝试搜索其他类似问题,但仍然找不到解决方案。
def plot(mwant, rwant, fwant):
data_list = query()
plt.clf()
y_err, x_err, x_col, y_col, f_col = [], [], [], [], []
markers = ["v", "^", "<", ">", "8", "s", "h", "H"]
i = 0
# create plot from query
for row in data_list:
x1, x2, y1, y2, flux = row[12], mwant, row[15], rwant, row[18]
x_err_low, x_err_upper = row[14], row[13]
y_err_low, y_err_upper = row[17], row[16]
distance = sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
if distance < 10:
x_col.append(x1)
y_col.append(y1)
f_col.append(row[18])
x_err.append([row[14], row[13]])
y_err.append([row[17], row[16]])
plt.scatter([x1], y1, c=[flux], vmin=0, vmax=1000, marker=markers[i % 8])
# plt.errorbar(x1, y1, xerr=[[x_err_low], [x_err_upper]])
i += 1
# plot user's point
plt.scatter([mwant], rwant, c=[fwant], vmin=0, vmax=1000)
plt.errorbar(x_col,
y_col,
xerr=np.array(x_err).T,
yerr=np.array(y_err).T, # Requires 2xN array
linestyle="None")
# options
plt.xlabel("Planet Mass")
plt.ylabel("Planet Radius")
plt.colorbar(ticks=[0, 500, 1000])
return plt
但是定位有偏差:
下面是如何添加非对称误差线的简单示例:
import numpy as np
from matplotlib import pyplot as plt
# generate data
x = np.linspace(0, 1, 5)
y = np.cos(x)
# generate errors
err = np.linspace(0.1, 0.25, 5)
lower_err = err * 0.5
upper_err = err
# put lower and upper values it to the same list
asym_err = [lower_err, upper_err]
plt.scatter(x, y)
plt.errorbar(x, y, yerr=asym_err, xerr=asym_err)
剧情:
问题是,错误栏中有负值。为了解决这个问题,我在错误栏中使用它之前围绕所有值做了一个 abs() 。
x_col.append(x1)
y_col.append(y1)
f_col.append(row[18])
x_err_low.append(abs(row[14]))
x_err_high.append(abs(row[13]))
y_err_low.append(abs(row[17]))
y_err_high.append(abs(row[16]))
误差线:
plt.errorbar(np.array(x_col),
np.array(y_col),
xerr=[x_err_low, x_err_high],
yerr=[y_err_low, y_err_high], # Requires 2xN array
linestyle="None")
我正在尝试创建一个带有不对称误差线的散点图,但误差线的位置偏离了。它应该只是 x_err=[[low_value_error], [max_value_error]] 但我似乎无法让它工作。我尝试搜索其他类似问题,但仍然找不到解决方案。
def plot(mwant, rwant, fwant):
data_list = query()
plt.clf()
y_err, x_err, x_col, y_col, f_col = [], [], [], [], []
markers = ["v", "^", "<", ">", "8", "s", "h", "H"]
i = 0
# create plot from query
for row in data_list:
x1, x2, y1, y2, flux = row[12], mwant, row[15], rwant, row[18]
x_err_low, x_err_upper = row[14], row[13]
y_err_low, y_err_upper = row[17], row[16]
distance = sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
if distance < 10:
x_col.append(x1)
y_col.append(y1)
f_col.append(row[18])
x_err.append([row[14], row[13]])
y_err.append([row[17], row[16]])
plt.scatter([x1], y1, c=[flux], vmin=0, vmax=1000, marker=markers[i % 8])
# plt.errorbar(x1, y1, xerr=[[x_err_low], [x_err_upper]])
i += 1
# plot user's point
plt.scatter([mwant], rwant, c=[fwant], vmin=0, vmax=1000)
plt.errorbar(x_col,
y_col,
xerr=np.array(x_err).T,
yerr=np.array(y_err).T, # Requires 2xN array
linestyle="None")
# options
plt.xlabel("Planet Mass")
plt.ylabel("Planet Radius")
plt.colorbar(ticks=[0, 500, 1000])
return plt
但是定位有偏差:
下面是如何添加非对称误差线的简单示例:
import numpy as np
from matplotlib import pyplot as plt
# generate data
x = np.linspace(0, 1, 5)
y = np.cos(x)
# generate errors
err = np.linspace(0.1, 0.25, 5)
lower_err = err * 0.5
upper_err = err
# put lower and upper values it to the same list
asym_err = [lower_err, upper_err]
plt.scatter(x, y)
plt.errorbar(x, y, yerr=asym_err, xerr=asym_err)
剧情:
问题是,错误栏中有负值。为了解决这个问题,我在错误栏中使用它之前围绕所有值做了一个 abs() 。
x_col.append(x1)
y_col.append(y1)
f_col.append(row[18])
x_err_low.append(abs(row[14]))
x_err_high.append(abs(row[13]))
y_err_low.append(abs(row[17]))
y_err_high.append(abs(row[16]))
误差线:
plt.errorbar(np.array(x_col),
np.array(y_col),
xerr=[x_err_low, x_err_high],
yerr=[y_err_low, y_err_high], # Requires 2xN array
linestyle="None")