Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contour graph in python

How would I make a countour grid in python using matplotlib.pyplot, where the grid is one colour where the z variable is below zero and another when z is equal to or larger than zero? I'm not very familiar with matplotlib so if anyone can give me a simple way of doing this, that would be great.

So far I have:

x= np.arange(0,361)
y= np.arange(0,91)

X,Y = np.meshgrid(x,y)

area = funcarea(L,D,H,W,X,Y) #L,D,H and W are all constants defined elsewhere.

plt.figure()
plt.contourf(X,Y,area)
plt.show()
like image 834
ajor Avatar asked Mar 24 '13 16:03

ajor


People also ask

What are contour levels Python?

MatPlotLib with Python Contour plots (sometimes called Level Plots) are a way to show a three-dimensional surface on a two-dimensional plane. It graphs two predictor variables X Y on the y-axis and a response variable Z as contours. These contours are sometimes called the z-slices or the iso-response values.

What is the purpose of Matplotlib and define contour plot with example?

Contour plots are widely used to visualize density, altitudes or heights of the mountain as well as in the meteorological department. Due to such wide usage matplotlib. pyplot provides a method contour to make it easy for us to draw contour plots.


1 Answers

You can do this using the levels keyword in contourf.

enter image description here

import numpy as np
import matplotlib.pyplot as plt

fig, axs = plt.subplots(1,2)

x = np.linspace(0, 1, 100)
X, Y = np.meshgrid(x, x)
Z = np.sin(X)*np.sin(Y)

levels = np.linspace(-1, 1, 40)

zdata = np.sin(8*X)*np.sin(8*Y)

cs = axs[0].contourf(X, Y, zdata, levels=levels)
fig.colorbar(cs, ax=axs[0], format="%.2f")

cs = axs[1].contourf(X, Y, zdata, levels=[-1,0,1])
fig.colorbar(cs, ax=axs[1])

plt.show()

You can change the colors by choosing and different colormap; using vmin, vmax; etc.

like image 117
tom10 Avatar answered Oct 13 '22 22:10

tom10