我目前正在尝试在 matplotlib 中创建一个 X by 3 系列图形,我最近做了类似的事情,但是这种特定的 2D 度量形式确实给我带来了删除轴标签或设置 MaxNLocator 的挑战。
目前,每个子批次仍会尝试自行显示 X 标签和 Y 标签。使用我正在处理的相同代码,我的 3 x 1 绘图或 1 x 1 绘图根本没有遇到这个问题。这似乎是特定于我通过 3 条路线走 X 的时候,我假设它与 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)
Run Code Online (Sandbox Code Playgroud)
我也尝试过这些
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)
Run Code Online (Sandbox Code Playgroud)
即使有这些尝试,我仍然不断得到这个。
该指标涵盖 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}]
Run Code Online (Sandbox Code Playgroud)
隐藏 x 轴(日期)上的标签并使 y 轴为整数而不是浮点数。
ax = plt.axes()
ax.plot(np.random.rand(50))
ax.yaxis.set_major_locator(plt.NullLocator())
ax.xaxis.set_major_formatter(plt.NullFormatter())
Run Code Online (Sandbox Code Playgroud)
这是我的测试代码(如果上面的链接对您没有帮助):
代码(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)
Run Code Online (Sandbox Code Playgroud)
使用:axarr[i,j].yaxis.set_major_locator(plt.MaxNLocator(integer=True))和
plt.subplots(nrows, ncols,sharex=True, sharey=True)上面一样。
要定义 y 轴上的范围,请使用:
axarr[i,j].set_ylim([0,max(your_y_axis_data_set)]) # change your_y_axis_data_set
Run Code Online (Sandbox Code Playgroud)
还可以通过差值,计算tick Difference(刻度偏差)
| 归档时间: |
|
| 查看次数: |
715 次 |
| 最近记录: |