[英]matplotlib subplots with shared axis
I have some troubles understanding how the matplotlib subplots allow for sharing axis between them.我在理解 matplotlib 子图如何允许它们之间共享轴时遇到了一些麻烦。 I saw some exemples but i could not modify one to fit my use case..;
我看到了一些例子,但我无法修改一个以适应我的用例..; Here i replaced my data by uniforms so the plots wont be interesting but whatever...
在这里,我用制服替换了我的数据,所以情节不会很有趣,但无论如何......
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib import cm
d = 4
n1 = 100000
n2 = 100
background_data = np.random.uniform(size=(n1,d))
foreground_data = np.random.uniform(size=(n2,d))
fig = plt.figure()
for i in np.arange(d):
for j in np.arange(d):
if i != j:
ax = fig.add_subplot(d,d,1+i*d+j)
ax = plt.hist2d(background_data[:, i], background_data[:, j],
bins=3*n2,
cmap=cm.get_cmap('Greys'),
norm=mpl.colors.LogNorm())
ax = plt.plot(foreground_data[:,i],foreground_data[:,j],'o',markersize=0.2)
Q : How can i share the x and y axes for all plots ?问:如何共享所有绘图的 x 和 y 轴?
By far the easiest option is to use sharex
and sharey
arguments of plt.subplots
.目前最简单的选择是使用
sharex
和sharey
的参数plt.subplots
。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
d = 4
n1 = 100000
n2 = 100
background_data = np.random.uniform(size=(n1,d))
foreground_data = np.random.uniform(size=(n2,d))
fig, axs = plt.subplots(d,d, sharex=True, sharey=True)
for i in np.arange(d):
for j in np.arange(d):
if i != j:
ax = axs[j,i]
ax.hist2d(background_data[:, i], background_data[:, j],
bins=3*n2,
cmap=plt.get_cmap('Greys'),
norm=mpl.colors.LogNorm())
ax.plot(foreground_data[:,i],foreground_data[:,j],'o',markersize=2)
else:
axs[j,i].remove()
fig.savefig("sharedaxes.png")
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.