python - 在 matplotlib 中以编程方式绘制重叠偏移图

标签 python matplotlib

我有 3 个不同的图,目前每个图都保存为单独的图形。然而,由于空间限制,我想将它们分层放置并像这样偏移:

Example image

我试图传达每个情节中都存在类似的模式,这是一种很好且紧凑的方式。我想使用 matplotlib 以编程方式绘制这样的图形,但我不确定如何使用常用的 pyplot 命令对图形进行分层和偏移。任何的意见都将会有帮助。以下代码是我目前的框架。

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

window = 100
xs = np.arange(100)
ys = np.zeros(100)
ys[80:90] = 1
y2s = np.random.randn(100)/5.0+0.5

with sns.axes_style("ticks"):
    for scenario in ["one", "two", "three"]:
        fig = plt.figure()
        plt.plot(xs, ys)
        plt.plot(xs, y2s)
        plt.title(scenario)
        sns.despine(offset=10)

最佳答案

您可以手动创建要绘制的轴并根据需要放置它们。 为了突出这种方法,请修改您的示例,如下

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

window = 100
xs = np.arange(100)
ys = np.zeros(100)
ys[80:90] = 1
y2s = np.random.randn(100)/5.0+0.5

fig = plt.figure()
with sns.axes_style("ticks"):
    for idx,scenario in enumerate(["one", "two", "three"]):
        off = idx/10.+0.1
        ax=fig.add_axes([off,off,0.65,0.65], axisbg='None')
        ax.plot(xs, ys)
        ax.plot(xs, y2s)
        ax.set_title(scenario)
        sns.despine(offset=10)

给出了类似的情节
enter image description here

在这里,我使用 fig.add_axes 将手动创建的坐标区对象添加到预定义的图形对象中。参数指定新创建的轴的位置和大小,请参阅 docs 。 请注意,我还将坐标区背景设置为透明 (axisbg='None')。

关于python - 在 matplotlib 中以编程方式绘制重叠偏移图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27869845/

相关文章:

python - 如何根据 HTTP 请求使用 Python 和 Flask 执行 shell 命令和流输出?

python - 在模拟过程中更新 matplotlib 图

python - seaborn 在 sublime 中的问题

python - Django 中的自动更新缓存

python - Python 的 os.walk 的 Ruby 等价物是什么?

python3 : having problems with os. 步行

python - 在 python 中读取 (SVHN) 数据集

python - 带有基于另一列的标记的 Pandas 线图

python-3.x - 我如何在 ReadTheDocs 中将 matplotlib 的 plot-directive 与 python-3 一起使用?

python - 如何在不从下到上对值进行排序的情况下更改绘图轴 (yaxis)