2

I have created following plot:

enter image description here

using following code:

dataframe_plot2.set_index('Class')[['HB ref', "HB tussenfase","HB raaigras"]].T.plot(kind='bar', stacked=True,width=0.1)
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))

However, the space between the x-axis ticks is rather large. I would like this distance to be smaller. What is an easy way to do this?

Numerous pages on the web explain how to make the space larger in very specific cases, but not how to decrease the space. e.g. How to change spacing between ticks in matplotlib?

A similar question has been asked before: Scatter plot: Decreasing spacing between scatter points/x-axis ticks however the answer provided there is not clear to me.

also checked:

but this did not provide me the information I need.

I'm also not looking for the tick frequency (like Changing the "tick frequency" on x or y axis in matplotlib?)

I tried:

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
dataframe_plot2.set_index('Class')[['HB ref', "HB tussenfase","HB raaigras"]].T.plot(kind='bar', stacked=True,width=0.1)
dataframe_plot2.xaxis.set_major_locator(plt.ticker.MultipleLocator(5))
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))

AttributeError: 'DataFrame' object has no attribute 'xaxis'

and

import matplotlib.pyplot as plt
import matplotlib.ticker as tic
dataframe_plot2.set_index('Class')[['HB ref', "HB tussenfase","HB raaigras"]].T.plot(kind='bar', stacked=True,width=0.1)
tic.MultipleLocator(5)
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))

but this has no effect.

UPDATE: Running:

import matplotlib.pyplot as plt
dataframe_plot2.set_index('Class')[['HB ref', "HB tussenfase","HB raaigras"]].T.plot(kind='bar', stacked=True,width=0.1)
plt.xlim(-2, 4)
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))

results in:

enter image description here

EDIT current and expected output:

Current:

enter image description here

Expected:

enter image description here

Robvh
  • 1,191
  • 1
  • 11
  • 22
  • 1
    Unlike for the case where you want to increase spacing, where there is only the option to make the figure larger, decreasing space between ticks could be done in several ways. One is the inverse of the above, namely to make the figure smaller, but one could also make the axes inside the figure smaller, or one could make the content within the axes smaller. It's not clear which of those you want here. – ImportanceOfBeingErnest Jul 01 '19 at 13:38
  • Thank you for your reply. Making the figure smaller is not what I'm looking for. Also making the axes smaller does not seem an option. What do you exactly mean with ''make the content within the axes smaller"? – Robvh Jul 01 '19 at 13:41
  • Currently the data range of the x axis is something like -0.3, 2.3. If instead you make it `plt.xlim(-2, 4)` the content would be shrunk. – ImportanceOfBeingErnest Jul 01 '19 at 13:46
  • @ImportanceOfBeingErnest I ran the code, it did make the space smaller (edited orginal question)... but it now has a lot of white space at the sides. Any thoughts? – Robvh Jul 01 '19 at 14:00
  • Well, sure, the space must be somewhere, right? Maybe you want to take a pen and paper and draw how it's currently, and how you would like to have it instead. Use the drawn picture to upload here, or at least to become clear about what you want. – ImportanceOfBeingErnest Jul 01 '19 at 14:25
  • I think it should be clear from my question. Let me try it another way: let's say that in my original graph the spacing between "HB ref" and "HB tussenfase" is 1, and also between "HB tussenfase" and "HB raaigras" is 1. Now I want that spacing to be, for example, 0.5, so that the bars are closer together. In that sense the margins of the plot also have to become smaller, else you get a lot of empty space on the right side of the graph. Is this more clear? – Robvh Jul 01 '19 at 14:59
  • So the plotting area (black line square) also has to become smaller. The Right vertical line has to be moved to the left. – Robvh Jul 01 '19 at 15:00
  • Keeping the figure (and axes) the same size leads you to two possiblities to decrease the space between the bars. (1) is to shift the outer bars towards the centre and increase whitespace at the sides - @ImportanceOfBeingErnest has shown you how to do that. (2) is to increase the width of the bars. You can do that by increasing the `width` kwarg in your plotting command from 0.1 to something bigger. – tmdavison Jul 01 '19 at 15:11
  • Let me just renew my proposal of showing a picture of desired vs. current output (possibly hand drawn). Else, this will not lead to anything. – ImportanceOfBeingErnest Jul 02 '19 at 14:05
  • @ImportanceOfBeingErnest Sorry for the late reply, I had a field workday yesterday. I added the current vs desired output. Are these drawings sufficient? – Robvh Jul 03 '19 at 10:20
  • @tmdavison thanks for the other option. I also tried this and indeed it does work. However, I would really like to know how to put bars of this size closer together. I edited my original question to visually show my expected output. – Robvh Jul 03 '19 at 10:21
  • 1
    You said earlier you didn't want to make the figure or axes smaller, but that's exactly what you have in your 'expected' picture. – tmdavison Jul 03 '19 at 10:33
  • I'm sorry, I guess I misunderstood! – Robvh Jul 03 '19 at 11:46

0 Answers0