Issue
In the following code, what is the right way to print the y ticks as E 40% instead of ('E', 40, '%') i.e. no brackets, quotation marks and no space between 40 and %?
from matplotlib import pyplot as plt
a = [20, 40, 60, 120, 160] # number of samples of each classes
b = ['A', 'B', 'C', 'D', 'E'] # different types of classes
c = [5, 10, 15, 30, 40] # percentage of samples in each classes
d = ['%' for i in range(0, len(a))]
e = list(zip(b, c, d))
plt.barh(b, a)
plt.xlabel('Number of Samples')
plt.ylabel('Different Classes')
plt.yticks(ticks = b, labels = e)
plt.show()
Solution
You're creating a tuple of the elements in b, c, and d in the list(zip(..))
command. What you should be doing is just combining them in some other way, which can easily be done in a list comprehension:
e = ['{:s} {:d}%'.format(b[i],c[i]) for i in range(len(a))]
To explain this, this code creates a string of the format "string value%" by using the values in your lists b and c. This also negates the use of the "%" list which is completely redundant since you use a percent sign in every list (if you use multiple symbols than this might be necessary, however, and you can simply put another {:s} field with the formatted d[i] value in the string above).
Subbing in the new list in your current code results in:
Answered By - jhso
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.