有人可以分享一个示例来创建4个散点图作为子图吗? 澄清。我打算创建一个pdf绘图。每页将有4个子图。每个子图都是散布直方图。
创建散布直方图的示例似乎是this
与使用此散点图示例并对每个子图进行子图绘制相比,是否还有其他替代函数可以用更少的行来完成此操作?
答案 0 :(得分:2)
使用链接的示例,您要做的就是增加subplots的数量。
然后为每个子图浏览示例代码,以使每个子图成为散布直方图。
我在下面粘贴了一个玩具示例:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
fig, axes = plt.subplots(figsize=(10,10),nrows=2, ncols=2)
print(axes)
colors = ['r','b','g','m']
for row in axes:
for axScatter in row:
print()
x = np.random.randn(1000)
y = np.random.randn(1000)
# the scatter plot:
# gets color from the end ('m' will be first)
color = colors.pop()
axScatter.scatter(x, y,color = color)
axScatter.set_aspect(1.)
# create new axes on the right and on the top of the current axes
# The first argument of the new_vertical(new_horizontal) method is
# the height (width) of the axes to be created in inches.
divider = make_axes_locatable(axScatter)
axHistx = divider.append_axes("top", 1.2, pad=0.1, sharex=axScatter)
axHisty = divider.append_axes("right", 1.2, pad=0.1, sharey=axScatter)
# make some labels invisible
axHistx.xaxis.set_tick_params(labelbottom=False)
axHisty.yaxis.set_tick_params(labelleft=False)
# now determine nice limits by hand:
binwidth = 0.25
xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))
lim = (int(xymax/binwidth) + 1)*binwidth
bins = np.arange(-lim, lim + binwidth, binwidth)
axHistx.hist(x, bins=bins,color=color)
axHisty.hist(y, bins=bins, orientation='horizontal',color=color)
# the xaxis of axHistx and yaxis of axHisty are shared with axScatter,
# thus there is no need to manually adjust the xlim and ylim of these
# axis.
axHistx.set_yticks([0, 50, 100])
axHisty.set_xticks([0, 50, 100])
plt.show()