【发布时间】:2020-01-08 03:21:33
【问题描述】:
我正在尝试为多个来源绘制列密度图“nh”。每个源都由一个键值表示。而且我有多个字典可以将该键与某个值相匹配。例如:
print(nh_noUL_val, '\n')
print(noUL_colors, '\n')
print(asymetric_error,'\n')
这给出了我的数据:
{2: 3.3e+21, 7: 7e+20, 29: 2e+22, 203: 8.5e+21, 226: 2.1e+21, 231: 6e+19, 259: 4.2e+21, 307: 1.8e+20, 320: 2.6e+21, 366: 4.4e+22, 374: 6e+21, 1143: 3e+22}
{2: 'black', 7: 'green', 29: 'red', 203: 'blue', 226: 'blue', 231: 'blue', 259: 'blue', 307: 'green', 320: 'green', 366: 'blue', 374: 'red', 1143: 'red'}
{2: [4e+20, 4e+20], 7: [7e+20, 3.6e+21], 29: [2e+22, 3.3e+22], 203: [8.5e+21, 2.47e+22], 226: [2.1e+21, 2.17e+22], 231: [6e+19, 9.76e+21], 259: [4.2e+21, 1.9899999999999997e+22], 307: [1.8e+20, 4.65e+21], 320: [2.6e+21, 1.2900000000000001e+22], 366: [4.4e+22, 1.4800000000000001e+23], 374: [6e+21, 3.1e+22], 1143: [3e+22, 4e+22]}
使用这些数据,我试图将每个源绘制为具有各自颜色、值和不对称误差的彩色数据点。
fig, ax = plt.subplots()
for x, y, yerr, color in zip(nh_noUL_val.keys(), nh_noUL_val.values(), asymetric_error.values(),
noUL_colors.values()):
ax.errorbar(x,y , yerr = asymetric_error, color = color, marker = 'o', ms = 5)
plt.show()
但是,这给了我:
ValueError: err must be [ scalar | N, Nx1 or 2xN array-like ]
诚然,我对 python 有点陌生,并不完全理解数组。但我的猜测是我的asymmetric_error.values() 实际上是一个 Nx2 数组?如果是这种情况,我如何将其转换为 2xN 形式?如果不是这样,我的代码有什么问题?
【问题讨论】:
-
.., asymetric_error.values(), colors.values()):- 应该是...noUL_colors.values()):吗? -
好的,谢谢!我更改了变量名,后来忘记更正了。但问题依然存在。
-
这对你有什么帮助?如果没有定义,就不会像我没有工作一样打印它。
标签: python arrays dictionary matplotlib