Mar-30-2022, 03:24 PM
Because of the issue I posted about a couple weeks (?) ago regarding matplotlib's asymmetric plotting of x-axis labels (for me, they were at locations of the 1st and 22nd of each month), I'm trying a different approach. Here's some code:
len(a) = 20. If I want 3 total tick labels then I divide 20 / 3 = 6.67, round that down to 6, and count every sixth week including the first: 2017-01-06, 2017-2-17, 2017-3-31, 2017-5-12. Whoops, that's four tick labels not three! This is exactly what I get, too. //4 plots 4 tick labels and //5 plots 5 tick labels as I'd like. //6 gives 7 tick labels, though.
I suspect the problem here is with the floor division operation, but I need to have an integer divisor for %.
The following works for 1-5 but dividing by 6 or 7 both plot 7 tick labels::
import matplotlib a = ['2017-01-06', '2017-01-13', '2017-01-20', '2017-01-27', '2017-02-03', '2017-02-10', '2017-02-17', \ '2017-02-24', '2017-03-03', '2017-03-10', '2017-03-17', '2017-03-24', '2017-03-31', '2017-04-07', \ '2017-04-14', '2017-04-21', '2017-04-28', '2017-05-05', '2017-05-12', '2017-05-19'] b = [433, 1216, 1234, 2175, 2825, 3271, 3240, 2979, 2234, 3114, 3139, 2975, 3831, 3758, 4127, 4900, 4216,\ 4785, 4043, 4346] fig, ax = plt.subplots(1) ax.plot(a,b) ax.xaxis.set_major_locator(matplotlib.ticker.FixedLocator([x for x in range(len(a)) if x%(len(a)//3)== 0])) plt.xticks(rotation=90) plt.show()What I'm trying to get is n tick labels where n is the divisor as shown in the L10 list comprehension.
len(a) = 20. If I want 3 total tick labels then I divide 20 / 3 = 6.67, round that down to 6, and count every sixth week including the first: 2017-01-06, 2017-2-17, 2017-3-31, 2017-5-12. Whoops, that's four tick labels not three! This is exactly what I get, too. //4 plots 4 tick labels and //5 plots 5 tick labels as I'd like. //6 gives 7 tick labels, though.
I suspect the problem here is with the floor division operation, but I need to have an integer divisor for %.
The following works for 1-5 but dividing by 6 or 7 both plot 7 tick labels::
ax.xaxis.set_major_locator(matplotlib.ticker.FixedLocator([x for x in range(len(a)) if x%(round(len(a)/6,0))== 0]))Can you articulate what I'm overlooking in my logic? Thanks!