如何从多个子批次中迭代删除X轴标签

2024-04-19 11:42:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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,但始终会遇到错误


Tags: supportdataaxstaffdoctorsetlocatorsns
1条回答
网友
1楼 · 发布于 2024-04-19 11:42:05
  1. 展平axes
  2. 创建一个listy_cols,其中包含要用于y的所有列
  3. 遍历axesy_cols
  • 下面的代码使用工作示例中的df运行
  • 使用python 3.8.11pandas 1.3.1matplotlib 3.4.2seaborn 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()

enter image description here


  • Seaborn只是matplotlib的一个高级API
  • 或者,使用^{},因为您正在绘制数据帧。
    • 如果未指定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)) 

enter image description here

enter image description here

相关问题 更多 >