栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

matplotlib:具有不同比例的叠加图?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

matplotlib:具有不同比例的叠加图?

听起来您想要的只是子图…您现在正在做的事情没有多大意义(或者我对您的代码片段感到非常困惑,无论如何……)。

尝试更多类似这样的方法:

import matplotlib.pyplot as pltimport numpy as npfig, axes = plt.subplots(nrows=3)colors = ('k', 'r', 'b')for ax, color in zip(axes, colors):    data = np.random.random(1) * np.random.random(10)    ax.plot(data, marker='o', linestyle='none', color=color)plt.show()

编辑:

如果您不想使用子图,则您的代码段会更有意义。

您正在尝试在彼此的正上方添加三个轴。Matplotlib正在认识到图中的大小和位置已经存在一个子图,因此每次都返回 相同的
轴对象。换句话说,如果您查看列表

ax
,就会发现它们都是 同一对象

如果 确实 要这样做,则

fig._seen
每次添加轴时都需要将其重置为空字典。但是,您可能真的不想这样做。

与其将三个独立的图放在彼此之上,不如看看使用方法

twinx

例如

import matplotlib.pyplot as pltimport numpy as np# To make things reproducible...np.random.seed(1977)fig, ax = plt.subplots()# Twin the x-axis twice to make independent y-axes.axes = [ax, ax.twinx(), ax.twinx()]# Make some space on the right side for the extra y-axis.fig.subplots_adjust(right=0.75)# Move the last y-axis spine over to the right by 20% of the width of the axesaxes[-1].spines['right'].set_position(('axes', 1.2))# To make the border of the right-most axis visible, we need to turn the frame# on. This hides the other plots, however, so we need to turn its fill off.axes[-1].set_frame_on(True)axes[-1].patch.set_visible(False)# And finally we get to plot things...colors = ('Green', 'Red', 'Blue')for ax, color in zip(axes, colors):    data = np.random.random(1) * np.random.random(10)    ax.plot(data, marker='o', linestyle='none', color=color)    ax.set_ylabel('%s Thing' % color, color=color)    ax.tick_params(axis='y', colors=color)axes[0].set_xlabel('X-axis')plt.show()



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/668028.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号