首页 > 解决方案 > 无法隐藏子图轴标签或在 matplotlib 中设置 MaxNLocator

问题描述

我目前正在尝试在 matplotlib 中创建 X x 3 系列图形,我最近也做过类似的事情,但是这种特定的 2D 形式的度量确实给我带来了删除轴标签或设置 MaxNLocator 的挑战。

目前,每个子批次仍然尝试自己显示 X 标签和 Y 标签。使用我正在处理的相同代码,我的 3 x 1 绘图或 1 x 1 绘图根本不会遇到这个问题。它似乎是特定于我何时走 X 3 路线并且我假设它与 2D 相关。

这是我目前正在尝试的。因为目前“团队”的数量在波动,所以我创建了比我需要的更多的地块并删除了未使用的地块。我可以稍后改进,但我更担心标签。

plt.rcParams['figure.figsize'] = [18, 10]
fig, ax = plt.subplots(nrows=10, ncols=3)
for number, team in enumerate(team_dict.keys()):
    print(number,team)
    df = pd.DataFrame(data=team_dict[team])
    axy = ax[number // 3][number % 3]
    df = pd.pivot_table(df,values='count_events',index=['day'],columns=['level'])
    axy = df.plot(ax=axy)
    axy.legend().set_visible(False)
    axy.yaxis.set_major_locator(MaxNLocator(integer=True))
    axy.xaxis.label.set_visible(False)

我也尝试过这些

for main_axis in ax:
    for axis in main_axis:
        if axis.lines:
            axis.get_xaxis().label.set_visible(False)
            axis.get_yaxis().set_major_locator(MaxNLocator(integer=True))
            axis.legend().set_visible(False)

        if not axis.lines:
            axis.set_visible(False)

即使有这些尝试,我仍然不断得到这个。

示例指标

该指标涵盖 90 天的数据。所以我只想把X轴全部隐藏起来。对于 Y 轴,我只想强制使用整数。我试过这样做并隐藏它无济于事。出于某种原因,在这种 2d 格式中,我似乎根本无法操作子图标签。

这是我的字典的一个小样本

team_dict['Team1']
[{'day': datetime.datetime(2019, 4, 1, 19, 31, 46, 606217),
  'level': '5',
  'count_events': 1},
 {'day': datetime.datetime(2019, 4, 2, 19, 31, 46, 606217),
  'level': '5',
  'count_events': 1},
 {'day': datetime.datetime(2019, 4, 3, 19, 31, 46, 606217),
  'level': '5',
  'count_events': 1},
 {'day': datetime.datetime(2019, 4, 4, 19, 31, 46, 606217),
  'level': '5',
  'count_events': 1}]

team_dict['Team2']
[ {'day': datetime.datetime(2019, 3, 29, 19, 31, 46, 606217),
  'level': '4',
  'count_events': 11},
 {'day': datetime.datetime(2019, 3, 30, 19, 31, 46, 606217),
  'level': '4',
  'count_events': 11},
 {'day': datetime.datetime(2019, 3, 31, 19, 31, 46, 606217),
  'level': '4',
  'count_events': 11},
 {'day': datetime.datetime(2019, 4, 1, 19, 31, 46, 606217),
  'level': '4',
  'count_events': 11},
 {'day': datetime.datetime(2019, 4, 2, 19, 31, 46, 606217),
  'level': '4',
  'count_events': 11},
 {'day': datetime.datetime(2019, 4, 3, 19, 31, 46, 606217),
  'level': '4',
  'count_events': 11},
 {'day': datetime.datetime(2019, 4, 4, 19, 31, 46, 606217),
  'level': '4',
  'count_events': 10}]

标签: pythonmatplotlib

解决方案


To hide the labels on x-axis (dates) and making the y-axis an integer instead of floating points.

ax = plt.axes()
ax.plot(np.random.rand(50))

ax.yaxis.set_major_locator(plt.NullLocator())
ax.xaxis.set_major_formatter(plt.NullFormatter())

Here my test code (if above links did not help you):
Code (Jupyter Notebook)

import matplotlib.pyplot as plt
import numpy as np
nrows = 3
ncols = 4
f, axarr = plt.subplots(nrows, ncols,sharex=True, sharey=True)

for i in range(nrows):
    for j in range(ncols):
        axarr[i,j].plot(np.random.rand(50))
        #axarr[i,j].axis('off')
        axarr[i,j].yaxis.set_major_locator(plt.MaxNLocator(integer=True))
        axarr[i,j].xaxis.set_major_formatter(plt.NullFormatter())
f.suptitle("This is the title for whole figure", fontsize=16)

Output:
enter image description here

Use: axarr[i,j].yaxis.set_major_locator(plt.MaxNLocator(integer=True)) and plt.subplots(nrows, ncols,sharex=True, sharey=True) as above.

To define the range on y-axis use:

axarr[i,j].set_ylim([0,max(your_y_axis_data_set)]) # change your_y_axis_data_set

You can also pass the difference, calculate the tick difference (tick deviation)


推荐阅读