Переменная размер маркера панели ошибок - PullRequest
0 голосов
/ 25 сентября 2019

Я хочу построить некоторые данные x и y, в которых мне нужно, чтобы размер маркера зависел от третьего массива z.Я мог бы построить их отдельно (то есть, разбросать x и y с size = z, и панель ошибок без маркера, fmc = 'none'), и это решает это.Проблема в том, что мне нужна легенда для отображения панели ошибок И точки вместе:

enter image description here

, а не

enter image description here

Здесь приведен код с некоторыми подготовленными данными:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(1,10,100)
y = 2*x
yerr = np.random(0.5,1.0,100)
z = np.random(1,10,100)

fig, ax = plt.subplots()

plt.scatter(x, y, s=z, facecolors='', edgecolors='red', label='Scatter') 
ax.errorbar(x, y, yerr=yerr, xerr=0, fmt='none', mfc='o', color='red', capthick=1, label='Error bar')

plt.legend()

plt.show()

, который выдает легенду, которую я хочу избежать:

enter image description here

In errorbar the argument markersize does not accept arrays as scatter` делает.

1 Ответ

1 голос
/ 25 сентября 2019

Идея обычно заключается в том, чтобы использовать прокси для добавления в легенду.Поэтому, хотя на панели ошибок на графике может отсутствовать маркер, на маркере есть набор маркеров.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(1,10,11)
y = 2*x
yerr = np.random.rand(11)*5
z = np.random.rand(11)*2+5

fig, ax = plt.subplots()

sc = ax.scatter(x, y, s=z**2, facecolors='', edgecolors='red') 
errb = ax.errorbar(x, y, yerr=yerr, xerr=0, fmt='none', 
                   color='red', capthick=1, label="errorbar")

proxy = ax.errorbar([], [], yerr=[], xerr=[], marker='o', mfc="none", mec="red", 
                    color='red', capthick=1, label="errorbar")
ax.legend(handles=[proxy], labels=["errorbar"])
plt.show()
...