简体   繁体   English

Matplotlib 散点图 Errorbar 偏离位置

[英]Matplotlib Scatter plot Errorbar way off position

I am trying to create an scatter plot with an asymmetrical error bars but the positioning for the error bar is way off.我正在尝试创建一个带有不对称误差线的散点图,但误差线的定位离得很远。 It should just be x_err=[[low_value_error], [max_value_error]] but I just cannot seems to get it to work.它应该只是 x_err=[[low_value_error], [max_value_error]] 但我似乎无法让它工作。 I have tried searching other similar problem but still cannot find a solution.我已经尝试搜索其他类似的问题,但仍然找不到解决方案。

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

but the positioning is way off:但定位很远:

ERROR_BAR 图像

Here is simple the example how to add asymmetric error bars:这是如何添加非对称误差条的简单示例:

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)

Plot:阴谋:

错误

The problem was, I had negative values in the errorbar.问题是,我在错误栏中有负值。 To fixed this, I did an abs() around all the values before I used it in the errorbar.为了解决这个问题,我在错误栏中使用它之前对所有值进行了 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]))

The errorbar:错误栏:

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

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

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