Matplotlib 将新轴添加到轴的右/左/右上角

Matplotlib 将新轴添加到轴的右/左/右上角,matplotlib,Matplotlib,如何将一个轴添加到另一个轴的外侧,使其作为一个整体保持在图形中legend和colorbar都具有此功能,但实现方式相当复杂(对我来说,很难复制)。您可以使用subplot命令来实现这一点,它可以像py.subplot(2,2,1)一样简单,其中前两个数字描述绘图的几何结构(2x2)第三个是当前的绘图编号。一般来说,最好像下面的示例中那样显式 import pylab as py # Make some data x = py.linspace(0,10,1000) cos_x = py.co

如何将一个轴添加到另一个轴的外侧,使其作为一个整体保持在图形中
legend
colorbar
都具有此功能,但实现方式相当复杂(对我来说,很难复制)。

您可以使用subplot命令来实现这一点,它可以像
py.subplot(2,2,1)
一样简单,其中前两个数字描述绘图的几何结构(2x2)第三个是当前的绘图编号。一般来说,最好像下面的示例中那样显式

import pylab as py

# Make some data
x = py.linspace(0,10,1000)
cos_x = py.cos(x)
sin_x = py.sin(x)

# Initiate a figure, there are other options in addition to figsize
fig = py.figure(figsize=(6,6))

# Plot the first set of data on ax1
ax1 = fig.add_subplot(2,1,1)
ax1.plot(x,sin_x)

# Plot the second set of data on ax2
ax2 = fig.add_subplot(2,1,2)
ax2.plot(x,cos_x)

# This final line can be used to adjust the subplots, if uncommentted  it will remove  all white space
#fig.subplots_adjust(left=0.13, right=0.9, top=0.9, bottom=0.12,hspace=0.0,wspace=0.0)

请注意,这意味着像
py.xlabel
这样的东西可能无法按预期工作,因为您有两个轴。相反,您需要指定
ax1.setxlabel(“…”)
,这使代码更易于阅读

可以找到更多的例子