-2

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()

enter image description here

joelostblom
  • 25,984
  • 12
  • 108
  • 120
Gonzalo
  • 924
  • 3
  • 15
  • 33
  • What about a [mcve] of the issue? – ImportanceOfBeingErnest Oct 18 '17 at 15:38
  • ?! Provided description , code, print.. – Gonzalo Oct 18 '17 at 15:39
  • 1
    You are asking me or whoever would be inclined to answer this to produce some `df1` himself. That is not very nice of you, given that you are the one asking for help here. – ImportanceOfBeingErnest Oct 18 '17 at 15:40
  • I am not asking you nothing, if you dont want to help dont help. – Gonzalo Oct 18 '17 at 15:46
  • 1
    You definitely need to add some sample data. In the absence of that, all I'll do is link you to a tutorial in the docs for the development version of matplotlib: http://matplotlib.org/devdocs/tutorials/intermediate/legend_guide.html#legend-location – Paul H Oct 18 '17 at 15:54
  • Invest some time with the Matplotlib tutorials to get a feel for how it all works. – wwii Oct 18 '17 at 16:02
  • Can't find on documentation nothing related to have the x-labels visible inside the bars instead of outside the chart. That's why I am here asking for help. – Gonzalo Oct 18 '17 at 16:23
  • You will get help by supplying a [mcve]. Do you mean [something like this](https://i.stack.imgur.com/YuVm8.png)? – ImportanceOfBeingErnest Oct 18 '17 at 17:35
  • Yes, that’s right. – Gonzalo Oct 18 '17 at 17:52
  • @ImportanceOfBeingErnest if you didn’t want to help at all don’t know why you keep commenting and showing snips. I will discover how to do it alone, no worries. There is people really mean. – Gonzalo Oct 18 '17 at 18:42
  • You could try to decrease the fontsize, something like `ax.set_xticklabels(ax.get_xticklabels(), fontsize=10)`. Or use `df1.plot.barh` to make a horizontal barplot. I would not put the text inside the bars, that would be difficult to see. I can post an answer with these examples if you think it will be helpful. – joelostblom Oct 18 '17 at 22:16
  • @JoelOstblom if you could provide the examples would help. Thank you. – Gonzalo Oct 19 '17 at 06:58

1 Answers1

1

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)

enter image description here

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)

enter image description here

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])

enter image description here

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])

enter image description here

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')

enter image description here

joelostblom
  • 25,984
  • 12
  • 108
  • 120