How to get list of exactly 10 items? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to get list of exactly 10 items? (/thread-37317.html) |
How to get list of exactly 10 items? - Mark17 - May-26-2022 I'm getting a traceback on this line: axs[1].set_xticks(list(range(1,len(btstats.index),len(btstats.index)//(len(xtick_labels_converted)-1))),xtick_labels_converted)ValueError: The number of FixedLocator locations (9), usually from a call to set_ticks, does not match the number of ticklabels (10). btstats has a row for every trading day over three years. xtick_labels_converted is a list of 10 evenly spaced dates across the 3-year interval including the starting and ending date. .set_xticks(a,b) has a as tick locations (a list) and b as tick labels (also a list). The idea is to get evenly spaced date labels along the x-axis to include all 10 dates in xtick_labels_converted. I fiddled around with this and got it to work with the -1. What I didn't realize is that while it worked for that particular len(btstats.index), it doesn't work for all as the size of btstats changes. I'm realizing that now with this diagnostic: for i in range(11,721,5): a = list(range(1,i,i//(len(xtick_labels_converted)))) print(f'Length of list is {len(a)}: {a}')Floor division obviously isn't working out because depending on the length of list a, I'm getting length of list anywhere between 10-15. I need exactly 10 to avoid that ValueError mismatch. Dividing by 10 would leave a variable remainder most times and being a date, the 10th label can't go in variable places... it has to come n days after 1, which is 1/2/2017. What's a better way to ensure I get 10 evenly spaced tick locations including the first and last? RE: How to get list of exactly 10 items? - Mark17 - May-26-2022 This seems to work: axs[0].set_xticks(list(np.linspace(1,len(btstats.index),num=len(xtick_labels_converted))),xtick_labels_converted)Hopefully that's a stable solution. |