子图若干散点直方图

2024-09-28 20:59:51 发布

您现在位置:Python中文网/ 问答频道 /正文

有人可以分享一个例子来创建4个散点历史图作为子图吗? 澄清。我计划创建一个pdf格式的绘图。每一页将有4个子版块。每个子图都是散点直方图。在

创建散点直方图的示例似乎是this

有没有其他的函数可以用更少的行来完成这项工作,而不是使用这个散点图示例并对每一行进行细分?在


Tags: 函数绘图示例pdf格式历史直方图this
1条回答
网友
1楼 · 发布于 2024-09-28 20:59:51

使用链接的示例,您只需增加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()

相关问题 更多 >