2

I want to present a 2D matrix, like in below for example:

2 3 4 5 6 3
1 2 2 4 5 5
1 2 2 2 2 4

I use contourf in matplotlib,to do the work,

cmap = colors.ListedColormap(['0.75', (0.2, 0.3, 0.5), 'r', 'b', 'g', 'c'])
bounds = [-0.5, 0.5, 1.5, 2.5, 3.5, 4.5]
norm = colors.BoundaryNorm(bounds, cmap.N)

map = plt.contourf(x, y, Cordi, cmap=cmap, norm=norm, 
                   level=[1,2,3,4,5], vmin=1, vmax=6)

Now here is the problem, there are so many ways to set the colors, I want to know which is in charge of what and who has the priority.

  1. In my colormap, there is a bounds which limits the color in a special range

  2. In contourf, there is a level which controls the same thing

  3. In controuf, there is a vmin and vmax which also controls the same thing

I know imshow is more proper for my case and it has already worked successfully. But now when it comes to contourf, there is a level inside, which just makes me confused.

Thank you for viewing my question.

rankthefirst
  • 1,370
  • 2
  • 14
  • 26

1 Answers1

2

with the help of this post,

matplotlib standard colormap usage

I kind of figure out.

there are two parts in the whole process. one is contourf, one is colormap.

for colormap, define colors and normalize them. This part decides what color should be used at which time, if this colormap is chosen to plot.

for contourf, level deals with which part you want to plot. It seems, vmin and vmax is of little use in this part. Or maybe I haven't find some.

for example,

first defines a cmap

import matplotlib.pyplot as plt
from matplotlib import colors
cmap1 = plt.cm.jet

cmap = colors.ListedColormap(['r','b','g'])
bounds = [0,1,2,3]
norm = colors.BoundaryNorm(bounds, cmap.N)

a = plt.contourf([[1, 1], [3, 3],[5,5]], cmap = cmap, \
norm = norm, levels = [0,1,2])
plt.colorbar()

plt.show()

this means between [0,1], the color will be red, and between [1,2], will be blue, and so on.

Then, if you define a level in contourf, e.g. [0,1,2] first, the program will categorize your data into different parts according to your level. In this the program only cares about [0,1,2], because you leveled it before.

And between [0,1],the mid point is 0.5, refering to the cmap, it should be red, but look at the data, there is no data between [0,1], so go on.

when it comes to [1,2], the mid point should be 1.5, it should be blue, according to the cmap. and data exist in this range, so the data is plotted.

Here, it is the end of the level. the program stops, although you have data over 2. enter image description here

And if I change the level to [0,1,2,3,4,5,6,7], the result is as below. So when the level exceeds the cmap, the exceeding part will show the same color. In this case, when the value exceeds 3, there is no color defined in the colormap dealing with value over 3, so it stays the same color as in range [2,3]. enter image description here

And now change bounds to [1,2,3,4] and level to [0.6,1.5,2.5,3.5], the result is following, enter image description here

So, first interval, [0.6,1.5], the mid point is 1.05, according to cmap, it should be red. And the rest parts follow the same rule.

Community
  • 1
  • 1
rankthefirst
  • 1,370
  • 2
  • 14
  • 26