I have the following plot created with matplotlib
genres_list=['Action', 'Adventure', 'Animation', 'Children', 'Comedy', 'Crime', 'Documentary', 'Drama', 'Fantasy', 'Horror', 'Musical', 'Mystery', 'Romance', 'Sci-Fi', 'Thriller', 'War', 'Western']
model_evaluation=np.array([0. , 0. , 0. , 0. , 0.8366437 ,
0. , 0. , 0.73629546, 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. ], dtype=np.float32)
fig=plt.figure()
ax=fig.add_subplot(111)
plt.rcParams["figure.figsize"] = (10,8)
plt.xticks(range(len(genres_list)), rotation=0, ha='center', fontsize=8)
plt.grid(b=True)
color_labels="black"
ax.bar(genres_list, model_evaluation)
ax.set(xlabel='Movie Genre', ylabel='F1 score', title='F1 score per movie genre', xticklabels=genres_list)
ax.spines['left'].set_color(color_labels)
ax.spines['bottom'].set_color(color_labels)
ax.tick_params(axis='x', colors=color_labels, which='major', pad=8)
ax.tick_params(axis='y', colors=color_labels)
ax.yaxis.label.set_color(color_labels)
ax.xaxis.label.set_color(color_labels)
ax.title.set_color(color_labels)
for i, f1 in enumerate(model_evaluation):
ax.annotate(round(f1, 2), (i, f1), ha='center', va='bottom')
plt.show()
Output:
As you can see the long genres eat space from the shorter genre names like in the Documentary genre which falls upon Crime and Drame.
I want to leave equal space from each genre but based on the length of the longest name, which is the Documentary genre.
Based on the links below:
- How to change spacing between ticks in matplotlib?
- Matplotlib increase spacing between points on x-axis
- How do I add space between the ticklabels and the axes in matplotlib?
I made the following code adjustments:
ax.tick_params(axis='x', colors=color_labels, which='major', pad=8, labelsize=6.5)
Output after the adjustment:
After the adjustment the genre Documentary is not equally spaced between its left and right genre.
So my question is how can place all the genres equally from one another based on the length of the longest genre (Documentary).