简体   繁体   中英

setting axis scale in matplotlib contour plot

I'm generating a contour plot in matplotlib based on output from an FEM program which gives nodal coordinates and their corresponding state parameters as follows:

x = [x1,x2,x3,...,xn] y = [y1,y2,y3,...,yn] z = [z1,z2,z3,...,zn]

Where z is a list.

The contour plot is generated as follows

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as ml

#paste data extraction routine here to generate list for x, y and z

xi = np.linspace(min(x),max(x),len(x))
yi = np.linspace(min(y), max(y), len(y))
zi = ml.griddata(x,y,z,xi,yi, interp = 'linear')

plt.figure()
contourplot = plt.contourf(xi,yi,zi, cmap=plt.cm.bone,
                  origin='lower')
cbar = plt.colorbar(contourplot)
plt.axis('equal')
plt.show()

The contour plot I'm getting depends on the second to last line plt.axis('equal')

If I include the line I get this:

在此处输入图片说明

If I don't include the line I get this:

在此处输入图片说明

The figure depicts deformation in a soil layer and I'd like to make sure that it plots to scale, and that I can control the resulting size of the image.

The first figure is to scale (ie the axes are in the same scale) but its not taking up the entire figure as can be seen.

The second figure is not to scale, but it takes up the whole figure.

I'd like to make sure that I get a proper to-scale image. Can I control the axes in this manner? Thanks

You probably want to use

plt.gca().set_aspect("equal")

instead of plt.axis('equal') . The result would look like

在此处输入图片说明

You may then also play with the figure size and the margins, eg

plt.figure(figsize=(8,2))
# ...
plt.tight_layout()

would result in

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM