开满天机
import matplotlib.pyplot as pltfrom mpl_toolkits.axes_grid1 import Divider, Sizefrom mpl_toolkits.axes_grid1.mpl_axes import Axescm = lambda d: d/2.54x, y = [1700.0, 1725.0, 1750.0], [0.0, -12.0, 0.0] # μmdx, dy = 50.0, 12.0# take margins into accountxmin, xmax = min(x)-dx*0.05, max(x)+dx*0.05ymin, ymax = min(y)-dy*0.05, max(y)+dy*0.05dx, dy = xmax-xmin, ymax-ymin# 5 μm data == 1 cm plotscale = 5/1xlen, ylen = dx/scale, dy/scale# Now we know the extents of our data and the axes dimension,# so we can set the Figure dimensions, taking borders into accountleft, right = 2, 1bot, top = 1.5, 1.5fig = plt.figure( figsize=(cm(left+xlen+right), cm(bot+ylen+top)), dpi=118)# change bg color to show so that one can measure the figure# and the axes when pasted into SO and do their math…fig.set_facecolor('xkcd:grey teal')########## Below is stolen from Matplotlib Fixed Size Axes########## (please don't ask me…)# Origin and size of the x axis and y axish = [Size.Fixed(cm(left)), Size.Fixed(cm(xlen))]v = [Size.Fixed(cm(bot)), Size.Fixed(cm(ylen))]divider = Divider(fig, (0.0, 0.0, 1., 1.), h, v, aspect=False)# NB: Axes is from mpl_toolkits.axes_grid1.mpl_axesax = Axes(fig, divider.get_position())ax.set_axes_locator(divider.new_locator(nx=1, ny=1))fig.add_axes(ax)######### Above is stolen from Matplotlib Fixed Size Axes Demo plt.plot(x,y)plt.grid()ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax), yticks=range(-12,1,3), xlabel='X/μm', ylabel='Y/μm', title='X vs Y, 1 cm on plot equals 5 μm')fig.suptitle('Figure dimensions: w = %.2f cm, h = %.2f cm.'%( left+xlen+right, bot+ylen+top))fig.savefig('Figure_1.png', # https://stackoverflow.com/a/4805178/2749397, Joe Kington's facecolor=fig.get_facecolor(), edgecolor='none')