I have created following plot:
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:
- https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.axes.Axes.tick_params.html
- https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.xticks.html
- https://pandas.pydata.org/pandas-docs/version/0.22/generated/pandas.DataFrame.plot.html
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:
EDIT current and expected output:
Current:
Expected: