Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to smooth matplotlib contour plot?

I have numpy array with this shape: (33,10). When I plot contour I get ugly image like this: enter image description here

while contour() doesn't seem to have any argument about smoothing or some sort of interpolation feature.

I somehow expected that tool which offers contour plot should offer smoothing too.
Is there straight forward way to do it in MPL?

like image 865
theta Avatar asked Sep 05 '12 04:09

theta


People also ask

How do you smooth a contour plot?

Choose the "smoothness" of the new plot via the parameter newpoints . A 3D-surf plot would be more suitable for very smooth color-shading. Just rotate it to a top-down view.

How do you do a contour plot in Matplotlib?

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.

How do I change the contour color in Matplotlib?

Color maps on contour plots The default color scheme of Matplotlib contour and filled contour plots can be modified. A general way to modify the color scheme is to call Matplotlib's plt. get_cmap() function that outputs a color map object. There are many different colormaps available to apply to contour plots.


1 Answers

As others have already pointed out, you need to interpolate your data.

There are a number of different ways to do this, but for starters, consider scipy.ndimage.zoom.

As a quick exmaple:

import numpy as np import scipy.ndimage import matplotlib.pyplot as plt  data = np.loadtxt('data.txt')  # Resample your data grid by a factor of 3 using cubic spline interpolation. data = scipy.ndimage.zoom(data, 3)  plt.contour(data) plt.show() 

enter image description here

like image 190
Joe Kington Avatar answered Sep 28 '22 12:09

Joe Kington