简体   繁体   中英

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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