Values will be given as a max() from a pandas data frame. For each item, I would like to get a rounded max value to create y-ticks for a matplot plot with the number of ticks = 10.
The data frame I am using is the official John Hopkins Covid Data. The preceding code returns the data frames categorized by Countries or States, Daily totals or cumulative, cases or deaths.
I have written code in the for loop that will convert the max, which could be over 20 million or as low as 6, to get the leading digit and add 1, then concatenate extra zero's if needed. I would rather have a value rounded down if the next digit is small, as this code creates small gaps at the top of some charts.
is the code I wrote that converts back and forth between str and int pythonic? Is there a simple way to add a round method to that code? or Is there just a better, more efficient way to do what I'm trying to do?
# Per Capita ## (identical version for daily totals on dfs1)
cumulative2 = dfs2.T[default[ind]]
daily_cases2 = cumulative2.diff()
d_max2 = daily_cases2.max().max()
c_max2 = cumulative2.max().max()
...
plot1 = daily_cases1.plot(kind='area', stacked=False, ax=ax1, lw=2, ylim=(0, d_max1))
plot2 = daily_cases2.plot(kind='area', stacked=False, ax=ax2, lw=2, ylim=(0, d_max2))
plot3 = cumulative1.plot(kind='area', stacked=False, ax=ax3, lw=2, ylim=(0, c_max1))
plot4 = cumulative2.plot(kind='area', stacked=False, ax=ax4, lw=2, ylim=(0, c_max2))
plots = [plot1, plot2, plot3, plot4]
maxes = [d_max1, d_max2, c_max1, c_max2]
for i, plot in enumerate(plots):
rnd_max = int(f'{str(int(str(int(maxes[i]))[0]) + 1) + "0" * (len(str(int(maxes[i]))) - 1)}')
yticks = np.arange(0, rnd_max, 1 if rnd_max < 10 else rnd_max // 10)
ytick_labels = pd.Series(yticks).apply(lambda value: f"{int(value):,}")
plot.set_yticks(yticks)
plot.set_yticklabels(ytick_labels)
EDIT: The leading value I would like to be 3 if the value is 2,750,00 or 4 if the value is 41. So not a true base 10 return. but base 10 of with the leading digit.
cumulative:
State California Arizona Florida New York Texas Illinois
11/4/20 950920 250633 821123 519890 1003342 443803
3/14/20 372 12 76 557 60 64
5/22/20 90281 15624 49451 360818 53817 105444
daily:
State California Arizona Florida New York Texas Illinois
4/3/20 1226.0 173.0 1260.0 10675.0 771.0 1209.0
6/25/20 5088.0 3091.0 5004.0 814.0 5787.0 894.0
11/3/20 4990.0 1679.0 4637.0 2069.0 9721.0 6516.0
c_max and d_ max are just lists of floats/ints (equal to max value of pd series being plotted) 63817.0
2675262
Here's an output of a series of plots. You can see the first graph ticks go much higher than the actual max value of the first chart (ignore plot placement it's on the best fit for now). This is the result of rounding a low number high which I would like to alleviate. But the goal is to give the cleanest tick value I can while keeping the plots nice and tight