Why does '%0.0f%%' need a modulus after it? So far I know that the %0.0f removes all the numbers after the decimal point but what are the two '%%'for and why is a modulus required after it? The code by the way is for a histogram using matplotlib.pyplot. I am relatively new to python and seeking a simple explanation.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FormatStrFormatter
data = [0,1,5,9,8,6,7,8,1,2,3,4,5,6,9,8,7,10]
fig, ax = plt.subplots()
counts, bins, patches = ax.hist(data, facecolor='yellow', edgecolor='gray')
print(counts)
# Change the colors of bars at the edges...
twentyfifth, seventyfifth = np.percentile(data, [25, 75])
for patch, rightside, leftside in zip(patches, bins[1:], bins[:-1]):
if rightside < twentyfifth:
patch.set_facecolor('green')
elif leftside > seventyfifth:
patch.set_facecolor('red')
bin_centers = 0.5 * np.diff(bins) + bins[:-1]
print(np.diff(bins))
print(bins[:-1])
for count, x in zip(counts, bin_centers):
# Label the percentages
percent = '%0.0f%%' % (100 * float(count) / counts.sum())
ax.annotate(percent, xy=(x, 0), xycoords=('data', 'axes fraction'),
xytext=(0, -32), textcoords='offset points', va='top', ha='center')
# Give ourselves some more room at the bottom of the plot
plt.subplots_adjust(bottom=0.2)
plt.show()
Thanks for your help!