注解
Click here 下载完整的示例代码
散点直方图(可定位轴)¶
以直方图的形式在图的两侧显示散点的边缘分布。
为了使主轴与边缘对齐,轴位置由 Divider
,通过 make_axes_locatable
.
生成类似图形的另一种方法如中所示 直方图散点图 例子。下面所示的可定位轴方法的优点是边缘轴遵循主轴的固定纵横比。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# Fixing random state for reproducibility
np.random.seed(19680801)
# the random data
x = np.random.randn(1000)
y = np.random.randn(1000)
fig, ax = plt.subplots(figsize=(5.5, 5.5))
# the scatter plot:
ax.scatter(x, y)
# Set aspect of the main axes.
ax.set_aspect(1.)
# create new axes on the right and on the top of the current axes
divider = make_axes_locatable(ax)
# below height and pad are in inches
ax_histx = divider.append_axes("top", 1.2, pad=0.1, sharex=ax)
ax_histy = divider.append_axes("right", 1.2, pad=0.1, sharey=ax)
# make some labels invisible
ax_histx.xaxis.set_tick_params(labelbottom=False)
ax_histy.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)
ax_histx.hist(x, bins=bins)
ax_histy.hist(y, bins=bins, orientation='horizontal')
# the xaxis of ax_histx and yaxis of ax_histy are shared with ax,
# thus there is no need to manually adjust the xlim and ylim of these
# axis.
ax_histx.set_yticks([0, 50, 100])
ax_histy.set_xticks([0, 50, 100])
plt.show()

工具书类¶
本例中显示了以下函数、方法和类的使用:
import matplotlib
import mpl_toolkits
mpl_toolkits.axes_grid1.axes_divider.make_axes_locatable
matplotlib.axes.Axes.set_aspect
matplotlib.axes.Axes.scatter
matplotlib.axes.Axes.hist
出:
<function Axes.hist at 0x7faa00dc7f28>
关键词:matplotlib代码示例,codex,python plot,pyplot Gallery generated by Sphinx-Gallery