我花了很长时间研究如何让两个子图共享相同的y轴,并在Matplotlib中共享两个单色条.
正在发生的事情是,当我打电话给colorbar()在任一功能subplot1或者subplot2,它会自动缩放的情节使得彩条加的情节会适合进入"插曲"包围盒内部,造成两个并排侧地块是两个非常不同的大小.
为了解决这个问题,我尝试创建了第三个子图,然后我将其修改为不存在只有一个颜色条存在的图.唯一的问题是,现在这两个地块的高度和宽度是不均匀的,我无法弄清楚如何使它看起来没问题.
这是我的代码:
from __future__ import division
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import patches
from matplotlib.ticker import NullFormatter
# SIS Functions
TE = 1 # Einstein radius
g1 = lambda x,y: (TE/2) * (y**2-x**2)/((x**2+y**2)**(3/2))
g2 = lambda x,y: -1*TE*x*y / ((x**2+y**2)**(3/2))
kappa = lambda x,y: TE / (2*np.sqrt(x**2+y**2))
coords = np.linspace(-2,2,400)
X,Y = np.meshgrid(coords,coords)
g1out = g1(X,Y)
g2out = g2(X,Y)
kappaout = kappa(X,Y)
for i in range(len(coords)):
for …Run Code Online (Sandbox Code Playgroud) 我有以下情节:
import matplotlib.pyplot as plt
fig2 = plt.figure()
ax3 = fig2.add_subplot(2,1,1)
ax4 = fig2.add_subplot(2,1,2)
ax4.loglog(x1, y1)
ax3.loglog(x2, y2)
ax3.set_ylabel('hello')
Run Code Online (Sandbox Code Playgroud)
我希望能够创建轴标签和标题,不仅适用于两个子图中的每一个,还包括跨越两个子图的常用标签.例如,由于两个图都有相同的轴,我只需要一组x和y轴标签.我确实希望每个子图有不同的标题.
我尝试了一些东西,但没有一个能正常工作
我对这段代码的工作原理有点困惑:
fig, axes = plt.subplots(nrows=2, ncols=2)
plt.show()
Run Code Online (Sandbox Code Playgroud)
在这种情况下,无花果轴如何工作?它有什么作用?
为什么这不能做同样的事情:
fig = plt.figure()
axes = fig.subplots(nrows=2, ncols=2)
Run Code Online (Sandbox Code Playgroud)
谢谢
我开始使用matplot并管理一些基本情节,但现在我发现很难发现如何做我现在需要的东西:(
我的实际问题是如何将一个全局标题和全局图例放在带有子图的图形上.
我正在做2x3子图,我有很多不同颜色的图(大约200个).为了区分(大多数)我写的东西
def style(i, total):
return dict(color=jet(i/total),
linestyle=["-", "--", "-.", ":"][i%4],
marker=["+", "*", "1", "2", "3", "4", "s"][i%7])
fig=plt.figure()
p0=fig.add_subplot(321)
for i, y in enumerate(data):
p0.plot(x, trans0(y), "-", label=i, **style(i, total))
# and more subplots with other transN functions
Run Code Online (Sandbox Code Playgroud)
(对此有何看法?:))每个子图都有相同的样式功能.
现在我正试图获得所有子图的全局标题,以及解释所有样式的全球传奇.此外,我需要使字体很小,以适应那里的所有200种样式(我不需要完全独特的样式,但至少有一些尝试)
有人可以帮我解决这个任务吗?
我在matplotlib中制作了一组子图(例如,3 x 2),但我的数据集少于6个.如何将剩余的子图留空?
安排如下:
+----+----+
| 0,0| 0,1|
+----+----+
| 1,0| 1,1|
+----+----+
| 2,0| 2,1|
+----+----+
Run Code Online (Sandbox Code Playgroud)
这可能会持续几页,但在最后一页上,例如,2,1框中的5个数据集将为空.但是,我已将该数字声明为:
cfig,ax = plt.subplots(3,2)
Run Code Online (Sandbox Code Playgroud)
因此,在子图2,1的空间中,存在一组带有刻度和标签的默认轴.如何以编程方式将该空间渲染为空白且没有轴?
将行和列标题添加到循环中生成的子图网格中的最佳做法是matplotlib什么?我能想到一对,但不是特别整洁:
set_title()第一行.对于行,这不起作用.你必须text在地块之外画画.你能建议一个更好的选择吗?

可能重复:
MATLAB子图边距
在matlab中,在子图周围浪费了过多的空间.例如,在此示例中:
t = 0:0.001:2*pi+0.001;
figure(2);
for i = 1 : 25;
subplot(5,5,i);
plot(t, sin(i*t));
axis off
end
Run Code Online (Sandbox Code Playgroud)

图中超过50%的空间被浪费为"空白"我想缩小空白区域,但是找不到这样做的机制是不成功的.思考?
谢谢约翰
如果你想在较大的一个中插入一个小图,你可以使用Axes,就像这里一样.
问题是我不知道如何在子图中做同样的事情.
我有几个子图,我想在每个子图中绘制一个小图.示例代码将是这样的:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
for i in range(4):
ax = fig.add_subplot(2,2,i)
ax.plot(np.arange(11),np.arange(11),'b')
#b = ax.axes([0.7,0.7,0.2,0.2])
#it gives an error, AxesSubplot is not callable
#b = plt.axes([0.7,0.7,0.2,0.2])
#plt.plot(np.arange(3),np.arange(3)+11,'g')
#it plots the small plot in the selected position of the whole figure, not inside the subplot
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
提前致谢!
我正在尝试构造一个简单的函数,它接受一个subplot instance(matplotlib.axes._subplots.AxesSubplot)并将其投影转换为另一个投影,例如,转换为其中一个cartopy.crs.CRS投影.
这个想法看起来像这样
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
def make_ax_map(ax, projection=ccrs.PlateCarree()):
# set ax projection to the specified projection
...
# other fancy formatting
ax2.coastlines()
...
# Create a grid of plots
fig, (ax1, ax2) = plt.subplots(ncols=2)
# the first subplot remains unchanged
ax1.plot(np.random.rand(10))
# the second one gets another projection
make_ax_map(ax2)
Run Code Online (Sandbox Code Playgroud)
当然,我可以使用fig.add_subplot()功能:
fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(121)
ax1.plot(np.random.rand(10))
ax2 = fig.add_subplot(122,projection=ccrs.PlateCarree())
ax2.coastlines()
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有一个适当的matplotlib方法来改变定义后的子图轴投影.不幸的是,阅读matplotlib API没有帮助.
我正在使用subplot命令绘制5 x 3图,但每个子图周围都有很大的边距.
如何控制它们周围的边距大小?
有人可以帮忙吗?
figure;
for c=1:15
subplot(5,3,c);
imagesc(reshape(image(:,c), 360,480));
colormap gray;
axis image;
end
Run Code Online (Sandbox Code Playgroud)
