python - 没有共享轴的叠加图 : left-bottom vs. 右上角对 (python/matplotlib)

标签 python matplotlib

在 Python/Matplotlib 中,如何在同一个图表上绘制两个不共享任何轴的图?即,第一个图使用左轴和下轴,第二个图使用上轴和右轴。每对轴都是独立的,可用于在同一个图形上独立绘制多条曲线。 例如,这通常用于主成分分析的双图。

据我所知,我的解决方案不是通过 twinxtwiny,而且我还无法使用 host_suplot< 找到解决方案 两者都不是......我所有的研究都引导我找到具有共享轴的解决方案,但这些解决方案并没有解决这种特殊情况。 谢谢。

最佳答案

不是很优雅,但它可以工作。

import numpy as np
import matplotlib.pyplot as plt

x1 = np.arange(10)
y1 = x1**2
x2 = np.arange(100,200)
y2 = x2

fig = plt.figure()

ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8], label="ax1")
ax2 = fig.add_axes([0.1, 0.1, 0.8, 0.8], label="ax2", frameon=False)

ax1.yaxis.tick_left()
ax1.xaxis.tick_bottom()


ax2.yaxis.tick_right()
ax2.yaxis.set_label_position('right')
ax2.yaxis.set_offset_position('right')
ax2.xaxis.tick_top()
ax2.xaxis.set_label_position('top')

ax1.spines['right'].set_color('red')
ax1.spines['top'].set_color('red')

for ylabel, xlabel in zip(ax2.get_yticklabels(), ax2.get_xticklabels()):
     ylabel.set_color("red")
     xlabel.set_color("red")


ax1.plot(x1,y1)
ax2.plot(x2,y2, 'r')

plt.show()

enter image description here

关于python - 没有共享轴的叠加图 : left-bottom vs. 右上角对 (python/matplotlib),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21614974/

相关文章:

python - 请指导在推送到 Firebase 时循环通过 DataFrame

python - matplotlib 将 x 轴与自动缩放的 y 轴链接起来

python - 相当于用于谷歌存储的 python boto

python - 这段 python 代码有什么问题吗?输入后出现错误

python - matplotlib stepfilled 直方图在 xubuntu 的值 10^-1 处中断

python - 如何在 Colab 中显示 matplotlib 动画而不显示绘图?

python - Django 1.4.1 未加载管理站点(Django Book 教程)

python - 如何绘制预测值与真实值的关系图?

python - 合并图形或子图对象

python - Matplotlib.hist - 使用步进直方图平滑点之间的线