Reputation: 241
I am currently trying to create a X by 3 series of graphs in matplotlib, I have done similar things recently, but this specific 2D form of metrics is really giving me a challenge on removing axis labels or setting the MaxNLocator.
Currently, each sublot still tries to show the X labels and Y labels on their own. Using the same code I am working on, I am not running into this issue at all with my 3 x 1 plots or my 1 x 1 plots. It seems to be specific to when I go the X by 3 route and I assume its 2D related.
Here is what I am currently trying. Because the amount of 'teams' currently fluctuates, I create more plots than I need and remove the unused ones. I can improve this later, but I am more worried about the labels.
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)
I also attempted these
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)
Even with these attempts I still keep getting this.
This metric is to cover 90 days of data. So the X axis I just want to hide all together. With the Y axis, I want to just force whole numbers. I have tried doing that and hiding it to no avail. For some reason, in this 2d format, I can't seem to manipulate the subplot labels at all.
Here is a small sample of my dictionary
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}]
Upvotes: 6
Views: 1018
Reputation: 4684
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)
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)
Upvotes: 4