0

I have one questions about matplotlib and contourf.

I am using the last version of matplotlib with 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.

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.

In other works each part of the contourf plot correspond to one matrix. I am plotting decision surfaces of Machine Learning Models.

1

1 Answer 1

1

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()

enter image description here

You'll also see I've changed the alpha and zorder

I hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

@jdeJuan Iv edited my answer to give more clarity. Let me know if it works
sure. I am trying to get to a result and give feedback. Btw why do yo set to numpy.nan instead of 0?
@jdeJuan 0 will still show up in the plot whereas nan wont. I used it to make my answer more clear
ok, that was in fact what I was looking for. Putting nan where we do not want to plot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.