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

如何将次要轴上的网格线放在主要图的后面?

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

如何将次要轴上的网格线放在主要图的后面?

您所需的绘图顺序是(第一个是最后面的)

  • 轴网格
  • 双轴网格
  • 沿轴绘制
  • 双轴绘图

但是,正如评论所见,这是不可能的

您不能将一个轴上的艺术家的绘图顺序与另一个轴上的艺术家的绘图顺序交错

这意味着您需要4个轴而不是2个轴。

  • 原始y刻度网格的轴
  • 次级y刻度网格的轴
  • y轴上的绘图轴
  • 次要y刻度上的绘图轴

可能看起来像这样:

import matplotlib.pyplot as pltimport numpy as npnp.random.seed(42)foo = np.random.randn(1000)fig, ax1a = plt.subplots()  # ax1a for the histogram gridax2a = ax1a.twinx()         # ax2a for the cumulative step gridax1b = ax1a.twinx()         # ax1b for the histogram plotax2b = ax1a.twinx()         # ax2a for the cumulative step plot# link the respective y-axes for grid and plotax1a.get_shared_y_axes().join(ax1a, ax1b)ax2a.get_shared_y_axes().join(ax2a, ax2b)# Remove ticks and labels and set which side to labelticksoff = dict(labelleft=False, labelright=False, left=False, right=False)ax1a.tick_params(axis="y", **ticksoff)ax2a.tick_params(axis="y", **ticksoff)ax1b.tick_params(axis="y", labelleft=True, labelright=False, left=True, right=False)ax2b.tick_params(axis="y", labelleft=False, labelright=True, left=False, right=True)# Spines offfor ax in [ax1a, ax2a, ax1b]:    for k,v in ax.spines.items():        v.set_visible(False)ax1b.hist(foo, bins=50)ax2b.hist(    foo, bins=50, density=True, cumulative=True, histtype="step", color="tab:orange")ax1a.grid()ax2a.grid()plt.show()



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

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

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