在matplotlib中的颜色栏中添加标记或线条 [英] Adding markers or lines to colorbar in matplotlib

查看:51
本文介绍了在matplotlib中的颜色栏中添加标记或线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几行代码来生成热图( pcolormesh ).

I have the following lines of code to generate a heatmap (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()

它产生这个图像:

我要放置:

  1. 颜色条上的标记为平均值(0.186).
  2. 颜色栏上的一条RMS值为(0.295)的水平线.

有输入吗?

推荐答案

这比预期的要容易.我没有意识到colorbar在 plt.colorbar().ax

That was easier than expected. I didn't realize colorbar has a plottable axis in 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

看起来 x 轴是 (0, 1),y 轴是 (dataMin, dataMax) (0, 1).

Looks like the x-axis is (0, 1) and y-axis (dataMin, dataMax) (0, 1).

这篇关于在matplotlib中的颜色栏中添加标记或线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆