My x-axis ticklabels (the ones below graph) are stealing valuable space from the overall figure. I have tried to reduce its size by changing the text rotation, but that doesn't help much since the text labels are quite long.
Is there a better approach for reducing the space taken up by the xticklabel area? For instance, could I display this text inside the bars? Thanks for your support.
My code for graph settings is:
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.sans-serif'] = "Century Gothic"
matplotlib.rcParams['font.family'] = "Century Gothic"
ax = df1.plot.bar(x = '', y = ['Events Today', 'Avg. Events Last 30 Days'], rot = 25, width=0.8 , linewidth=1, color=['midnightblue','darkorange'])
for item in ([ax.xaxis.label, ax.yaxis.label] +
ax.get_xticklabels() + ax.get_yticklabels()):
item.set_fontsize(15)
ax.legend(fontsize = 'x-large', loc='best')
plt.tight_layout()
ax.yaxis.grid(True, which='major', linestyle='-', linewidth=0.15)
ax.set_facecolor('#f2f2f2')
plt.show()
When I end up with unaesthetically long xticklabels, the first and most important thing I do is to try to shorten them. It might seems evident, but it's worth pointing out that using an abbreviation or different description is often the simplest and most effective solution.
If you are stuck with long names and a certain fontsize, I recommend making a horizontal barplot instead. I generally prefer horizontal plots with longer labels, since it is easier to read text that is not rotated (which might also make it possible to reduce the fontsize one step further) Adding newlines can help as well.
Here is an example of an a graph with unwieldy labels:
import pandas as pd
import seaborn as sns # to get example data easily
iris = sns.load_dataset('iris')
means = iris.groupby('species').mean()
my_long_labels = ['looooooong_versicolor', 'looooooooog_setosa', 'looooooooong_virginica']
# Note the simpler approach of setting fontsize compared to your question
ax = means.plot(kind='bar', y=['sepal_length', 'sepal_width'], fontsize=15, rot=25)
ax.set_xlabel('')
ax.set_xticklabels(my_long_labels)
I would change this to a horizontal barplot:
ax = means.plot(kind='barh', y=['sepal_length', 'sepal_width'], fontsize=15)
ax.set_ylabel('')
ax.set_yticklabels(my_long_labels)
You could introduce newlines in the labels to further improve readability:
ax = means.plot(kind='barh', y=['sepal_length', 'sepal_width'], fontsize=15, rot=0)
ax.set_ylabel('')
ax.set_yticklabels([label.replace('_', '\n') for label in my_long_labels])
This also works with vertical bars:
ax = means.plot(kind='bar', y=['sepal_length', 'sepal_width'], fontsize=15, rot=0)
ax.set_xlabel('')
ax.set_xticklabels([label.replace('_', '\n') for label in my_long_labels])
Finally, you could also have the text inside the bars, but this is difficult to read.
ax = means.plot(kind='barh', y=['sepal_length', 'sepal_width'], fontsize=15)
ax.set_ylabel('')
ax.set_yticklabels(my_long_labels, x=0.03, ha='left', va='bottom')