Displayed ticks on x-axis in Matplotlib using the bar plot

Viewed 46

I am trying to plot histogram data with pandas and matplotlib. I am using the dataframe.plot() function. The problem is, that this generates a graph with a lot of displayed ticks on the x-axis. histogram plot

The index of the dataframe is used as ticks. Here is the code:

fig, axes = plt.subplots(figsize=(12,12))

PLOT_CONFIG_HISTOGRAM={
    "subplots":True,
    "title":"cyclictest wakeup latency",
    "legend":True,
    "xlabel":"latency/us",
    "ylabel":"cycles",
    "logy":True,
    "kind":"bar",
}

my_dataframe.plot(ax=axes,**PLOT_CONFIG_HISTOGRAM)

Is there a way in matplotlib or pandas to reduce the displayed ticks? Or a better way to display histogram data?

In the end should be something like this:

| | | | |
|       |
1       5

or like this:

|       |
1       5

Thanks in advance for any input.

2 Answers

You could either use a fixed number, e.g. 20, of bins for your plot:

PLOT_CONFIG_HISTOGRAM={
    [...]
    "bins": 20,
    [...]
}

Or if you want to keep the histograms resolution but want to lower the resolution of the labels of your x-axis, e.g. show only every 50th tick:

div = 50
locs, labels = plt.xticks()
plt.xticks(locs[::div], labels[::div])

You can set the number of ticks you want by specifying the number of bins in locator_params:

plt.locator_params(axis='x', nbins=10)

To clean up your plot further, you can also rotate the xticks and set the xlimits as below:

plt.xticks(rotation = '90')
plt.xlim(a,b) #where a and b represent the minimum and maximum of your x values
Related