[英]Matplotlib normalize colorbar (Python)
I'm trying to plot a contourf-plot using matplotlib (and numpy of course). 我正在尝试使用matplotlib绘制一个contourf-plot(当然还有numpy)。 And it works, it plots what it should plot, but unfortunatelly I cannot set the colorbar range.
它有效,它绘制了应该绘制的内容,但不幸的是我无法设置颜色条范围。 The problem is that I have a plenty of plots and need all of them to have the same colorbar (same min and max, same colors).
问题是我有很多情节,需要所有颜色都有相同的颜色条(相同的最小和最大,相同的颜色)。 I copy&past-ed almost every code snippet I found on the internet, but without success.
我复制并覆盖了我在互联网上找到的几乎所有代码片段,但没有成功。 My code so far:
我的代码到目前为止:
import numpy as np;
import matplotlib as mpl;
import matplotlib.pyplot as plt;
[...]
plotFreq, plotCoord = np.meshgrid(plotFreqVect, plotCoordVect);
figHandler = plt.figure();
cont_PSD = plt.contourf(plotFreq, plotCoord, plotPxx, 200, linestyle=None);
normi = mpl.colors.Normalize(vmin=-80, vmax=20);
colbar_PSD = plt.colorbar(cont_PSD);
colbar_PSD.set_norm(normi);
#colbar_PSD.norm = normi;
#mpl.colors.Normalize(vmin=-80, vmax=20);
plt.axis([1, 1000, -400, 400]);
As you can see there are three different lines for the colorbar norm, none of them is working. 正如您所看到的,colorbar规范有三种不同的行,但它们都不起作用。 The range is still set automatically... I mean everything else is working, why not the colorbar?
范围仍然自动设置...我的意思是其他一切都在工作,为什么不是colorbar? I don't even get errors or warnings.
我甚至没有收到错误或警告。
Thanks, itpdg 谢谢,itpdg
EDIT 1: Pictures, with plt.clim(-80,20): 编辑1:图片,plt.clim(-80,20):
add this after plt.colorbar()
: 在
plt.colorbar()
之后添加:
plt.clim(minimal_value, maximal_value)
for the contour plot, add the args vmin and vmax: 对于等高线图,添加args vmin和vmax:
cont_PSD = plt.contourf(plotFreq, plotCoord, plotPxx, 200, linestyle=None,vmin=minimal_value,vmax=maximal_value)
You complete code should work like this : 您完成的代码应该像这样工作:
import numpy as np;
import matplotlib as mpl;
import matplotlib.pyplot as plt;
[...]
plotFreq, plotCoord = np.meshgrid(plotFreqVect, plotCoordVect);
figHandler = plt.figure();
cont_PSD = plt.contourf(plotFreq, plotCoord, plotPxx, 200, linestyle=None,vmin=minimal_value,vmax=maximal_value);
plt.colorbar()
plt.clim(minimal_value,maximal_value)
plt.show()
I ran into this issue a while back and thought it was a bug (see MPL issue #5055 ). 我不久前遇到了这个问题并认为这是一个错误(参见MPL问题#5055 )。 It's not, but it does require using the
extend
kwarg, which was non-intuitive to me. 它不是,但确实需要使用
extend
kwarg,这对我来说是不直观的。 Here's what you want to do: 这是你想要做的:
normi = mpl.colors.Normalize(vmin=-80, vmax=20)
cont_PSD = plt.contourf(plotFreq, plotCoord, plotPxx,
np.linspace(-80, 20, 200),
linestyle=None,
norm=normi, extend='both')
plt.colorbar(colbar_PSD)
You can do-away with the plt.clim
, colbar_PSD.set_norm
and other similar calls. 您可以使用
plt.clim
, colbar_PSD.set_norm
和其他类似的调用来解决问题。
More examples uses of extend=
are available here . 有关
extend=
更多示例用法,请点击此处 。
Note that this will create a colorbar with 'triangles' at the top and bottom indicating that the data extends beyond the colorbar, but I think you'll like them once you get used to them, they are descriptive. 请注意,这将创建一个颜色条,顶部和底部带有“三角形”,表示数据超出了颜色条,但我认为一旦习惯它们就会喜欢它们,它们是描述性的。
Good luck! 祝好运!
Please user the levels
parameter, a set of examples: 请使用
levels
参数,一组示例:
In [9]:
ndom
z = np.random.random((10,10))
Without levels
, colorbar will be auto-scaled 没有
levels
,colorbar将自动缩放
In [11]:
plt.contourf(z)
plt.colorbar()
Out[11]:
<matplotlib.colorbar.Colorbar at 0x120d47390>
In [12]:
plt.contourf(z*2)
plt.colorbar()
Out[12]:
<matplotlib.colorbar.Colorbar at 0x120f6ac10>
Control colorbar with explicit levels
用明确的
levels
控制colorbar
In [13]:
plt.contourf(z*2, levels=np.linspace(0,2,20))
plt.colorbar()
Out[13]:
<matplotlib.colorbar.Colorbar at 0x121b119d0>
In [14]:
plt.contourf(z, levels=np.linspace(0,2,20))
plt.colorbar()
Out[14]:
<matplotlib.colorbar.Colorbar at 0x120dc3510>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.