首页 > 解决方案 > 如何在多个图中隐藏轴

问题描述

请问这个隐藏右轴和上轴的代码有什么问题?

import matplotlib.pyplot as plt

fig, ax = plt.subplots(sharex=True, sharey=True, figsize=(10,3))
fig1 = plt.subplot(121)
fig2 = plt.subplot(122)

# Set width of axes
for figures in [fig1, fig2]:
    # Removing axis 
    for side in ['right','top']:
        ax.spines[side].set_visible(False)
plt.show()

这适用于非多重情节:

for side in ['right','top']:
    ax.spines[side].set_visible(False)

编辑代码:

import matplotlib.pyplot as plt
import seaborn as sns

fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True, sharey=True, figsize=(10,3))
fig1 = plt.subplot(121)
ax1.set_xlabel(r'$k$')
ax1.set_ylabel(r'$\omega$', rotation='horizontal')

fig2 = plt.subplot(122)

sns.despine()
plt.show()

标签: matplotlibplotaxis

解决方案


推荐阅读