【发布时间】:2018-03-06 19:51:09
【问题描述】:
我知道这个话题已经在几个 matplotlib 博客中讨论过了,但我仍然找不到解决方案。我想创建一个这样的情节:
(来自@ImportanceOfBeingErnest 的回答)
所以这是两个子图,它们与我定义的 y 限制共享相同的 y 轴。 x 轴的单位应该与 y 轴的单位相同,这意味着如果我要画一个圆,它实际上就是一个圆。
现在我还想指定 x-limits 并喜欢子图大小以调整到此限制,但我无法使其工作。一般的问题似乎是 matplotlib 总是保持不同子图的 fig-size 。
这是一个最小的工作示例:
import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
ax1=fig.add_subplot(1,2,1,aspect='equal')
ax2=fig.add_subplot(1,2,2,aspect='equal',sharey=ax1)
def create_data(xmin,xmax):
delta = 0.025
----
x = np.arange(xmin, xmax, delta)
y = np.arange(-3,3,delta)
X, Y = np.meshgrid(x, y)
Z1 = plt.mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = plt.mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10 * (Z1 - Z2)
----
nr, nc = Z.shape
----
# put NaNs in one corner:
Z[-nr//6:, -nc//6:] = np.nan
# contourf will convert these to masked
----
----
Z = np.ma.array(Z)
# mask another corner:
Z[:nr//6, :nc//6] = np.ma.masked
----
# mask a circle in the middle:
interior = np.sqrt((X**2) + (Y**2)) < 0.5
Z[interior] = np.ma.masked
return X,Y,Z
X,Y,Z=create_data(-2,4)
ax1.contourf(X,Y,Z)
ax1.set_ylim(-1,1)
ax1.set_xlim(-2,4)
X,Y,Z=create_data(-1,0)
ax2.contourf(X,Y,Z)
ax2.set_ylim(-1,1)
ax2.set_xlim(-1,0)
plt.show()
在此示例中,y 轴是共享的,但 x 限制未正确应用:
此外,如何在与 y 轴对齐的子图右侧添加颜色条?
fig.colorbar(CS, ax=ax,shrink=xx)
似乎可以工作,但需要手动编辑收缩参数。
【问题讨论】:
-
我不认为我理解这个要求。如果两个子图的方面相等,并且您将一个图的 x 限制设置为另一个图的六分之一,那么最终图应该是什么样子。这至少与您显示的比例似乎是二分之一的图片不匹配。
-
它应该和@ImportanceOfBeingErnest 展示的完全一样,但是我无法重现他的情节。
标签: python matplotlib