【问题标题】:How to iteratively remove X axis labels from multiple subplots如何从多个子图中迭代地删除 X 轴标签
【发布时间】:2021-08-12 00:54:49
【问题描述】:

使用 seaborn 我创建了 21 个子图,我的代码如下:

fig, axes = plt.subplots(7, 3, figsize=(25, 25))

fig.suptitle('Workforce Statistics')

sns.lineplot(ax=axes[0, 0], data=dfStaff, x='Month', y='Central functions')
sns.lineplot(ax=axes[0, 1], data=dfStaff, x='Month', y='Support to ST&T staff')
sns.lineplot(ax=axes[0, 2], data=dfStaff, x='Month', y='Consultant')
sns.lineplot(ax=axes[1, 0], data=dfStaff, x='Month', y='Specialty Registrar')
sns.lineplot(ax=axes[1, 1], data=dfStaff, x='Month', y='Midwives')
sns.lineplot(ax=axes[1, 2], data=dfStaff, x='Month', y='Managers')
sns.lineplot(ax=axes[2, 0], data=dfStaff, x='Month', y='Ambulance staff')
sns.lineplot(ax=axes[2, 1], data=dfStaff, x='Month', y='Support to ambulance staff')
sns.lineplot(ax=axes[2, 2], data=dfStaff, x='Month', y='Senior managers')
sns.lineplot(ax=axes[3, 0], data=dfStaff, x='Month', y='Core Training')
sns.lineplot(ax=axes[3, 1], data=dfStaff, x='Month', y='Specialty Doctor')
sns.lineplot(ax=axes[3, 2], data=dfStaff, x='Month', y='Foundation Doctor Year 1')
sns.lineplot(ax=axes[4, 0], data=dfStaff, x='Month', y='Foundation Doctor Year 2')
sns.lineplot(ax=axes[4, 1], data=dfStaff, x='Month', y='Other staff or those with unknown classification')
sns.lineplot(ax=axes[4, 2], data=dfStaff, x='Month', y='Associate Specialist')
sns.lineplot(ax=axes[5, 0], data=dfStaff, x='Month', y='Hospital Practitioner / Clinical Assistant')
sns.lineplot(ax=axes[5, 1], data=dfStaff, x='Month', y='Other and Local HCHS Doctor Grades')
sns.lineplot(ax=axes[5, 2], data=dfStaff, x='Month', y='Staff Grade')
sns.lineplot(ax=axes[6, 0], data=dfStaff, x='Month', y='Nurses & health visitors')
sns.lineplot(ax=axes[6, 1], data=dfStaff, x='Month', y='Support to doctors, nurses & midwives')
sns.lineplot(ax=axes[6, 2], data=dfStaff, x='Month', y='HCHS doctors')

axes[0,0].xaxis.set_major_locator(MaxNLocator(6)) 
axes[0,1].xaxis.set_major_locator(MaxNLocator(6)) 
axes[0,2].xaxis.set_major_locator(MaxNLocator(6)) 
axes[1,0].xaxis.set_major_locator(MaxNLocator(6)) 
axes[1,1].xaxis.set_major_locator(MaxNLocator(6)) 
axes[1,2].xaxis.set_major_locator(MaxNLocator(6)) 
axes[2,0].xaxis.set_major_locator(MaxNLocator(6)) 
axes[2,1].xaxis.set_major_locator(MaxNLocator(6)) 
axes[2,2].xaxis.set_major_locator(MaxNLocator(6)) 
axes[3,0].xaxis.set_major_locator(MaxNLocator(6)) 
axes[3,1].xaxis.set_major_locator(MaxNLocator(6)) 
axes[3,2].xaxis.set_major_locator(MaxNLocator(6)) 
axes[4,0].xaxis.set_major_locator(MaxNLocator(6)) 
axes[4,1].xaxis.set_major_locator(MaxNLocator(6)) 
axes[4,2].xaxis.set_major_locator(MaxNLocator(6)) 
axes[5,0].xaxis.set_major_locator(MaxNLocator(6)) 
axes[5,1].xaxis.set_major_locator(MaxNLocator(6)) 
axes[5,2].xaxis.set_major_locator(MaxNLocator(6)) 
axes[6,0].xaxis.set_major_locator(MaxNLocator(6)) 
axes[6,1].xaxis.set_major_locator(MaxNLocator(6)) 
axes[6,2].xaxis.set_major_locator(MaxNLocator(6)) 

对于第二部分,我正在尝试创建一个 for 循环来迭代所有轴和 set_major_locator,但不断遇到错误。

【问题讨论】:

  • 希望答案是有帮助的。彻底回答问题很费时间。如果您的问题已解决,请接受解决方案。 ✔ 位于答案左上角的 ▲/▼ 箭头下方。如果出现更好的解决方案,则可以接受新的解决方案。如果您的声望超过 15,您还可以使用 ▲/▼ 箭头对答案的有用性进行投票。 如果解决方案无法回答问题,请发表评论。 What should I do when someone answers my question?。谢谢。

标签: python pandas matplotlib plot seaborn


【解决方案1】:
  1. 扁平化axes
  2. 创建一个list,y_cols,用于y的所有列
  3. 遍历axes 和y_cols
  • 以下代码使用 Working Example 中的 df 运行
  • 使用python 3.8.11、pandas 1.3.1、matplotlib 3.4.2 和seaborn 0.11.1 测试。
fig, axes = plt.subplots(3, 2, figsize=(12, 6))

# flatten axes into a 1D array, which is easier to iterate through
axes = axes.flatten()

# specify the y columns in a list
y_cols = df.columns[1:]

fig.suptitle('Workforce Statistics')

for ax, y in zip(axes, y_cols):
    
    sns.lineplot(ax=ax, data=df, x='Month', y=y)
    ax.set(title=y, ylabel='Something', xlabel='Date')

    ax.xaxis.set_major_locator(plt.MaxNLocator(5)) 
    
fig.tight_layout()


  • Seaborn 只是matplotlib 的高级 API
  • 或者,使用pandas.DataFrame.plot,因为您正在绘制数据框。
    • 如果未指定y=,则将绘制除'Month' 之外的所有列。否则,创建列列表并将其传递给y=y_cols
    • 使用matplotlib作为后端
axes = dfStaff.plot(x='Month', subplots=True, layout=(7, 3), figsize=(25, 25))
axes = axes.flatten()
for ax in axes:
    ax.xaxis.set_major_locator(plt.MaxNLocator(6)) 

工作示例

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# sample data
np.random.seed(365)
rows = 365*3
data = {'Month': pd.bdate_range('2017-01-10', freq='D', periods=rows),
        'a': np.random.randint(0, 10, size=(rows)),
        'b': np.random.randint(15, 25, size=(rows)),
        'c': np.random.randint(30, 40, size=(rows)),
        'd': np.random.randint(450, 550, size=(rows)),
        'e': np.random.randint(6000, 7000, size=(rows)),
        'f': np.random.randint(100, 201, size=(rows))}
df = pd.DataFrame(data)
df.head()

# display(df.head())
       Month  a   b   c    d     e    f
0 2017-01-10  2  17  36  480  6539  101
1 2017-01-11  4  18  30  482  6955  152
2 2017-01-12  1  16  30  504  6472  105
3 2017-01-13  5  17  32  519  6269  113
4 2017-01-14  2  17  37  534  6654  160

# plot
axes = df.plot(x='Month', subplots=True, layout=(2, 3), figsize=(15, 6), title='Workforce Statistics - with MaxNLocator', xlabel='Date')
axes = axes.flatten()
for ax in axes:
    ax.xaxis.set_major_locator(plt.MaxNLocator(6)) 

【讨论】:

    猜你喜欢
    • 2016-09-22
    • 2023-02-24
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多