简体   繁体   English

在matplotlib中向colorbar添加标记或线条

[英]Adding markers or lines to colorbar in matplotlib

I have the following lines of code to generate a heatmap ( pcolormesh ). 我有以下几行代码来生成热图( pcolormesh )。

import matplotlib.pyplot as plt
import numpy as np

vals = np.linspace(-np.pi/2, np.pi/2, 101)
x, y = np.meshgrid(vals, vals)

z = np.abs(np.sinc(x) * np.sinc(y))

xDeg = np.rad2deg(x)
yDeg = np.rad2deg(y)

plt.pcolormesh(xDeg, yDeg, z, cmap = 'jet', vmin = 0, vmax = 1)
plt.colorbar()

plt.axis([-90, 90, -90, 90])
ticks = np.linspace(-90, 90, 13)
plt.xticks(ticks)
plt.yticks(ticks)

print np.mean(z)                # 0.186225110029
print np.sqrt(np.mean(z**2))    # 0.295710882276
plt.show()

It produces this image: 它产生这个图像: pcolormesh结果

I'd like to place: 我想放置:

  1. A marker on the colorbar at the mean (0.186). 平均颜色条上的标记(0.186)。
  2. A horizontal line on the colorbar at the RMS value (0.295). 色条上的水平线,RMS值(0.295)。

Any input? 有什么输入?

That was easier than expected. 这比预期的要容易。 I didn't realize colorbar has a plottable axis in plt.colorbar().ax 我没有意识到colorbar在plt.colorbar().ax有一个可绘制的轴

cb = plt.colorbar()
cb.ax.plot(0.5, mean, 'w.') # my data is between 0 and 1
cb.ax.plot([0, 1], [rms]*2, 'w') # my data is between 0 and 1
# Note: would need to scale mean/rms to be between 0 and 1

Looks like the x-axis is (0, 1) and y-axis (dataMin, dataMax) (0, 1). 看起来像x轴是(0,1)和y轴 (dataMin,dataMax) (0,1)。

pcolormesh与colorbar上的标记。

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

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