简体   繁体   English

Python Matplotlib Contourf图

[英]Python matplotlib contourf plot

I have one questions about matplotlib and contourf. 我对matplotlib和Contourf有一个疑问。

I am using the last version of matplotlib with python3.7. 我正在使用matplotlib的最新版本和python3.7。 Basically I have to matrix I want to plot on the same contour plot but using different colormap. 基本上,我必须对要绘制在相同轮廓图上但使用不同颜色图的矩阵进行矩阵处理。 One important aspect is that, for instance, if we have zero matrixA and matrixB with shape=(10,10) then the positions in which matrixA is different of zero are the positions in which matrixB are non-zero, and viceversa. 一个重要方面是,例如,如果我们有零个矩阵A和矩阵B,它们的形状=(10,10),则矩阵A与零不同的位置就是矩阵B不为零的位置,反之亦然。

In other words I want to plot in different colors two different mask. 换句话说,我想以不同的颜色绘制两个不同的蒙版。

Thanks for your time. 谢谢你的时间。

Edited: 编辑:

I add an example here 我在这里加一个例子

import numpy
import matplotlib.pyplot as plt

matrixA=numpy.random.randn(10,10).reshape(100,)
matrixB=numpy.random.randn(10,10).reshape(100,)

mask=numpy.random.uniform(10,10)
mask=mask.reshape(100,)

indexA=numpy.where(mask[mask>0.5])[0]
indexB=numpy.where(mask[mask<=0.5])[0]

matrixA_masked=numpy.zeros(100,)
matrixB_masked=numpy.zeros(100,)
matrixA_masked[indexA]=matrixA[indexA]
matrixB_masked[indexB]=matrixB[indexB]

matrixA_masked=matrixA_masked.reshape(100,100)
matrixB_masked=matrixB_masked.reshape(100,100)

x=numpy.linspace(0,10,1)
X,Y = numpy.meshgrid(x,x)
plt.contourf(X,Y,matrixA_masked,colormap='gray')
plt.contourf(X,Y,matrixB_masked,colormap='winter')
plt.show()

What I want is to be able to use different colormaps that appear in the same plot. 我想要的是能够使用出现在同一图中的不同颜色图。 So for instance in the plot there will be a part assigned to matrixA with a contour color (and 0 where matrixB take place), and the same to matrixB with a different colormap. 因此,例如在图中,将有一个分配给轮廓颜色为matrixA的部分(其中发生matrixB时为0),相同的情况下分配给matrixB具有不同的色图。

In other works each part of the contourf plot correspond to one matrix. 在其他工作中,轮廓线图的每个部分都对应一个矩阵。 I am plotting decision surfaces of Machine Learning Models. 我正在绘制机器学习模型的决策面。

I stumbled into some errors in your code so I have created my own dataset. 我偶然发现了您的代码中的一些错误,因此我创建了自己的数据集。 To have two colormaps on one plot you need to open a figure and define the axes: 要在一个绘图上具有两个颜色图,您需要打开一个图并定义轴:

import numpy
import matplotlib.pyplot as plt

matrixA=numpy.linspace(1,20,100)
matrixA[matrixA >= 10] = numpy.nan
matrixA_2 = numpy.reshape(matrixA,[50,2])

matrixB=numpy.linspace(1,20,100)
matrixB[matrixB <= 10] = numpy.nan
matrixB_2 = numpy.reshape(matrixB,[50,2])

fig,ax = plt.subplots()
a = ax.contourf(matrixA_2,cmap='copper',alpha=0.5,zorder=0)
fig.colorbar(a,ax=ax,orientation='vertical')
b=ax.contourf(matrixB_2,cmap='cool',alpha=0.5,zorder=1)
fig.colorbar(b,ax=ax,orientation='horizontal')
plt.show()

在此处输入图片说明

You'll also see I've changed the alpha and zorder 您还将看到我更改了alphazorder

I hope this helps. 我希望这有帮助。

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

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