In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "Python how to draw percentage stack bar chart and fill pattern". In daily operation, I believe many people have doubts about how to draw percentage stack bar chart and fill pattern in Python. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the question of "how to draw percentage stack bar chart and fill pattern in Python". Next, please follow the editor to study!
Draw a percentage stacked bar chart through matplotlib in Python and set a different hatch pattern for each category. The main reason is that some papers are printed in black and white, and the distinction between different categories is not obvious, so I made this scheme.
There is a problem: do not know how to set the legend according to the hatch pattern, this article can be described as "curve to save the nation", the color block of the legend is set to white, so if someone knows how to set the legend according to hatching can be discussed, the original legend method does not provide such parameters.
The figure is as follows:
The code is as follows
Import numpy as npimport matplotlib.pyplot as pltimport matplotlib.patches as mpatchesimport matplotlib.ticker as mtickfrom matplotlib.ticker import PercentFormatter# sets the pattern of the hatch marks = ['Olympus first'] labels = [i for i in range (2010, 2021)] # data first = [42.85) 41.15, 39.41, 35.35, 351, 30.45, 29.81, 31.81, 32.41, 32.41, 30.42, 31.49] second = [23.20, 26.40, 27.7, 29.02, 31.49] others = [14.08, 12.9912.51, 11.54, 12.27, 12.9, 9.95] fourth = [16.14, 16.617, 17.619, 310, 18.40, 17.83, 19.1519.150.924.10] others = [3.73mer, 3.282.92.82.72.81g, 2.72.81g, 2.72.81g. 53 data 2.67, 2.57, 2.57, 2.36, 2.51, 2.54] [first] Second, third, fourth, others] x = range (len (labels)) width = 0.3 initialize all bottom_ y elements to 0bottom_y = np.zeros (len (labels)) data = np.array (data) # to prepare for percentage calculation sums = np.sum (data, axis=0) j = 0figsize = 86figure, ax = plt.subplots (figsize=figsize) plt.rcParams ['font.sans-serif'] = [' SimHei'] for i in data: y = I / sums plt.bar (x, y, width) Hatch=np.array (marks) [j], bottom=bottom_y, color='white', edgecolor='black') bottom_y = y + bottom_y plt.xticks (x, labels) # plt.yticks (range (1), ylabel) legend_labels = ['o legend1','/ legend2','* legend3', 'legend4',r'\ legend5'] color= [' white', 'white' 'white'] patches = [mpatches.Patch (color=color [h], label= "{: s}" .format (legend_ labels [h]) for h in range (len (legend_labels))] ax = plt.gca () box = ax.get_position () # the vertical axis is set to the percentage plt.gca (). Yaxis.set_major_formatter (PercentFormatter (1)) ax.legend (handles=patches,ncol=1, bbox_to_anchor= (1)) Borderaxespad = 0.) # generate legend figure.subplots_adjust (right=0.7) jackers draw dashed lines for i in range (1,11,1): plt.axhline (y=i/10, linestyle='dashed', color='black', linewidth=0.5) labels = ax.get_xticklabels () + ax.get_yticklabels () # set the numeric label font [label.set_fontname ('Times New Roman') for label in labels] plt.savefig (ringing filename.svg' Format='svg') plt.show () so far The study on "how to draw a percentage stacked bar chart and fill a pattern with Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.