更改matplotlib子块的大小

2024-06-16 10:30:35 发布

您现在位置:Python中文网/ 问答频道 /正文

是否有一种简单的方法可以修改此代码,使绘图更大而不改变轴上的比例?

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()

enter image description here

如果我去掉plt.gca().set_aspect('equal', adjustable='box'),这些图的大小是合适的,但它们不能缩放。


Tags: importboxreturnmatplotlibdefasnpplt
1条回答
网友
1楼 · 发布于 2024-06-16 10:30:35

子块缩小,以便它们的外观相等。这似乎是人们所期望的;因此,还不清楚“更大”指的是什么。

你仍然可以把这个数字放大,例如

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

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

您也可以让轴缩放,只设置与数据相等的方面

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

enter image description here

最后,在彼此下方绘制子块也会使它们变大。所以你可以用plt.subplot(211)plt.subplot(212).enter image description here

相关问题 更多 >