0

I am having troubles with setting the x-ticks on my bar graph. I currently have 644 data points, so its not feasible to have a tick for each data point. Fortunately, these data points are broken out into sections (I have a list of 644 length, which is: ['a','a','a','a','a','a''a','a','b','b','b','b','b','b',...] )

I would like for my x-axis to show when these values begin and end. there are only 6-7 different values, so it shouldn't look too cluttered.

To be honest, I'm not really sure where to start looking for this problem. I'm looking at xticks documentation and I'm not really getting anything.

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(644)

plt.bar(np.arange(len(x)),x)
plt.xlabel('x', fontsize=5)
plt.ylabel('y', fontsize=5)

plt.xticks(range(len(x_list)),x_list)
plt.locator_params(axis='x',nbins=10) #just shows first 10..not correct
plt.title('title')
plt.show()

I would like the graph to show something along the lines of where 'a' starts and ends, where 'b' starts and ends, etc.

JohnD
  • 3,884
  • 1
  • 28
  • 40
  • 1
    You can set the ticks explicitly with `ax.xaxis.set_major_locator` and then get only the unique values with `set([a, a, b, b])` – Josh Herzberg Mar 26 '19 at 15:02
  • Possible duplicate of [How to set ticks on Fixed Position , matplotlib](https://stackoverflow.com/questions/17129947/how-to-set-ticks-on-fixed-position-matplotlib) ... or maybe [Changing the “tick frequency” on x or y axis in matplotlib?](https://stackoverflow.com/questions/12608788/changing-the-tick-frequency-on-x-or-y-axis-in-matplotlib) – wwii Mar 26 '19 at 15:09
  • 1
    @JoshHerzberg can you elaborate on how to use sets with major_set_locator? I'm not really getting anywhere with this. – Brian Chen Mar 27 '19 at 13:46
  • @BrianChen Since you just want to observe where the values of your list begin and end, you really just want to set the ticks at the unique values (ie where the values are different). So `set([a, a, a, b, b, b])` will return `(a, b)` and then pass that value to the set_major_locator and you should get ticks where the values change. – Josh Herzberg Mar 28 '19 at 15:31

0 Answers0