本文介绍了更改 matplotlib 子图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以修改此代码,以便在不更改轴上的比例的情况下绘制更大的图?

Is there an easy way to modify this code so that the plots are bigger without changing the scale on the axes?

import numpy as np
import matplotlib.pyplot as plt
import math
%matplotlib inline

a, c = -10, 10
x = np.linspace(a,c,100)
x = np.array(x)
def y(x): return np.arctan(x)

h = 0.0000001

def grad(x,h): return (y(x+h)-y(x))/h
m = grad(x,h)

plt.figure(1)
plt.subplot(121)
plt.plot(x, y(x), 'b')
plt.xlim([a,c])
plt.ylim([min(y(x)),max(y(x))])
plt.gca().set_aspect('equal', adjustable='box')

plt.subplot(122)
plt.plot(x,m,'b')
plt.xlim([a,c])
plt.ylim([min(m),max(m)])
plt.gca().set_aspect('equal', adjustable='box')

plt.subplots_adjust(wspace = 0.5)
plt.show()

如果我摆脱了 plt.gca().set_aspect('equal',Adjustable ='box'),这些地块的大小就不错了,但它们却无法按比例绘制.

If I get rid of plt.gca().set_aspect('equal', adjustable='box') the plots come out a decent size but they are not to scale.

推荐答案

子图缩小,以使它们的长宽相等.这似乎是需要的;因此并不清楚更大"是指什么.

The subplots are shrunk such that their aspect is equal. This seems to be desired; and thus it is not really clear what "bigger" refers to.

您仍然可以使图形更大,例如

You can still make the figure larger, e.g.

plt.figure(1, figsize=(12,2))

,然后使用 plt.subplots_adjust 调整边距和间距.

and then adjust the margins and spacings using plt.subplots_adjust.

您还可以让轴缩放,并且仅对数据设置相同的长宽比,

You can also let the axes scale and only set the equal aspect to the data,

plt.gca().set_aspect('equal', adjustable='datalim')

最后将子图相互绘制在下面,这也会使它们更大.所以你可以使用 plt.subplot(211)plt.subplot(212).

Finally plotting the subplots beneath each other makes them bigger as well. So you might use plt.subplot(211) and plt.subplot(212).

这篇关于更改 matplotlib 子图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:55