我正在尝试绘制一个图,其中点的颜色将在特定值范围内发生变化。 我认为我做到了,但我对图例有疑问。
我编写了下面的代码,其中创建了包含 0,1 之间 28 个值的数组“时间”。 之后我创建了一个颜色条,并将“时间”值与 rgba 值相对应。 最后我绘制了数据。
import numpy as np
import matplotlib.pyplot as plt
legends=['0106860101', '0150280101', '0150280301', '0150280401', '0150280501', '0150280601', '0150281101', '0205230301', '0205230401', '0205230501', '0205230601', '0301860101', '0405090101', '0693850501', '0693851201', '0722650101', '0742490101', '0742590301', '0764770101', '0764770401', '0782310101', '0794580601', '0803990101', '0803990201', '0803990301', '0803990401', '0803990501', '0803990601']
time = np.linspace(0,1,28)
plt.figure(figsize=(15,9))
x=[14.46, 34.79, 43.94, 55.81, 23.55, 17.43, 18.42, 55.6, 14.78, 15.0, 50.83, 46.4, 40.23, 30.17, 15.9, 10.93, 13.75, 26.63, 11.02, 15.66, 32.34, 22.11, 21.78, 25.64, 14.59, 19.08, 12.15, 13.32]
y=[3.596, 5.803, 4.066, 3.538, 10.0, 2.989, 3.175, 3.602, 3.459, 3.101, 3.751, 3.625, 4.343, 2.778, 2.114, 5.433, 2.549, 2.619, 4.66, 2.875, 3.774, 2.959, 2.5, 2.731, 3.623, 3.537, 2.303, 2.27]
ye=[0.38, 2.394, 0.322, 0.243, 3.162, 0.552, 0.682, 0.157, 0.956, 0.316, 0.169, 0.169, 0.215, 0.069, 0.065, 1.147, 0.188, 0.098, 0.281, 0.654, 0.101, 0.255, 0.073, 0.07, 0.343, 0.448, 0.356, 0.181]
s=plt.scatter(x,y,c=time,cmap='plasma',marker='None',facecolors='none')
clb = plt.colorbar(s)
time_color = clb.to_rgba(time)
for i in range(len(x)):
plt.errorbar(x[i],y[i],yerr=ye[i],linestyle='',c=time_color[i],marker
="o",mfc='none')
plt.xscale("linear")
plt.yscale("linear")
plt.axis([0,70,0,15])
plt.xlabel("$L_{x}^{total}/L_{Edd}$",fontsize='18')
plt.ylabel("$T_2$ (keV)",fontsize='18')
plt.legend(labels=legends)
我得到的结果是,除了第一个点之外,所有点都被标记为正确。 ('0106860101') 。 我试图上传我的情节的图像,但我没有上传,因为我还没有权利。
我该怎么做才能解决这个问题?
提前谢谢
最佳答案
只需将 legends
的相应条目分配给 labels
参数,同时将各个点绘制为
for i in range(len(x)):
plt.errorbar(x[i],y[i],yerr=ye[i],linestyle='',c=time_color[i],
marker="o",mfc='none', label=legends[i]) # labels added here
plt.xscale("linear")
plt.yscale("linear")
plt.axis([0,70,0,15])
plt.xlabel("$L_{x}^{total}/L_{Edd}$",fontsize='18')
plt.ylabel("$T_2$ (keV)",fontsize='18')
plt.legend()
关于python - 我使用 colorbar 的 matplotlib 中的绘图图例问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57191817/