1

I plotted a contourf plot with 9x11 points. When I plot the contourf plot then I expect to see lines between data points (since there is no other data in between the data points). But for example in the 0.9 level there are some parts (especially between x=2 and x=4) which are not linear. What can be the reason for that?

Contourf plot

plt.figure()
x=np.linspace(0,10,11)
y=np.linspace(0,10,11)
X,Y = np.meshgrid(x,y)
levels = np.arange(0,1.01,0.1)
norm = cm.colors.Normalize(vmax=1, vmin=0)
cmap = cm.PRGn

CS1 = plt.contourf(X, Y, data,levels=levels,cmap=cm.get_cmap(cmap, 
        len(levels) - 1),norm=norm)
plt.xticks(np.arange(11),np.arange(11))
plt.yticks(np.arange(11),np.arange(250,855,55))
plt.xlim([0,8])
plt.colorbar(CS1)
plt.grid()
plt.show()
  • Can you post some example code? – Cong Ma Apr 28 '17 at 09:00
  • "there are some parts (...) which are not linear" - you mean the interpolation between them renders the line in a non-smooth way? Look into [interpolation](http://stackoverflow.com/questions/12274529/how-to-smooth-matplotlib-contour-plot). – berna1111 Apr 28 '17 at 10:03

2 Answers2

1

It's supposed to be that way: contourf colours in the area between the lines and contour draws the lines. See the examples.

berna1111
  • 1,811
  • 1
  • 18
  • 23
1

Maybe the following plot helps to understand a contour plot better.

enter image description here

Here we plot a contour of an array with 3x3 points. The value of the middle point (6) is much larger than the other values. We chose levels of 3 and 5 where to plot the contour lines. Those lines are calculated by the contour as to interpolate the data.
Using more points will then allow to use more lines and make them look more smooth.

import matplotlib.pyplot as plt
import numpy as np

X,Y = np.meshgrid(np.arange(3), np.arange(3))
Z = np.array([[1,1,1],[2,6,2],[1,1,1]])

fig, ax=plt.subplots()
cs = ax.contour(X,Y,Z, levels=[3,5])
cs2 = ax.contourf(X,Y,Z, levels=[1,3,5,6], alpha=0.3)
plt.clabel(cs, cs.levels, inline=False)
plt.colorbar(cs2)
ax.scatter(X,Y)
for x,y,z in zip(X.flatten(), Y.flatten(), Z.flatten()):
    ax.text(x,y,z)

plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712