Get the App
SLTechnology News&Howtos  ›  Development  › 

How to draw a bar chart with python

Shulou Source: shulou.com Published: 2022-05-31 20:33:22 09月21日 Update

This article mainly introduces "how to draw a bar chart with python". In the daily operation, I believe that many people have doubts about how to draw a bar chart with python. The editor consulted all kinds of materials and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the question of "how to draw a bar chart with python". Next, please follow the editor to study!

In the process of setting up, some data are displayed in tables, so it is difficult to see the differences between the data and highlight the characteristics of the data, so I want to make a bar chart, which is specially recorded here and ready for later use.

Import numpy as npimport matplotlib.pyplot as pltimport matplotlib# specifies the default font matplotlib.rcParams ['font.sans-serif'] = [' FangSong'] matplotlib.rcParams ['font.family'] =' sans-serif' c101 = (8p7) matplotlib.rcParams ['font.family'] =' sans-serif' c101 = (21) 21pint 24PM10) c103 = (4p4p10) c103 = (4pyr4) c104 = (1meme 5pence3) c104 = (3pene 5pence3) c105 = (3pene 5pence3) c1055105 = (3mem1re0, 01Demo) c106 = (31Demo 01Demo 0) c106 = (12gmml0) 0) c106 = (122000g) 10c0 (10c0) ) c109 = (1) ind = np.arange (024) # the x locations for the groupswidth = 0.2 # the width of the bars fig,ax = plt.subplots () rects1 = ax.bar (ind + width, c101, width,color=' SkyBlue',align='edge', label='101') rects2 = ax.bar (ind + 2*width, c102, width,color='IndianRed',align='edge') Label='102') rects3 = ax.bar (ind + 3*width, c103, width, color='Cyan',align='edge', label='103') rects4 = ax.bar (ind + 4*width, c104, width, color='Magenta',align='edge', label='104') rects5 = ax.bar (ind + 5*width, c105, width, color='Purple',align='edge', label='105') rects6 = ax.bar (ind + 6*width, c106, width, color='Green',align='edge' Label='106') rects7 = ax.bar (ind + 7*width, c107, width, color='Yellow',align='edge', label='107') rects8 = ax.bar (ind + 8*width, c108, width, color='Blue',align='edge', label='108') rects9 = ax.bar (ind + 9*width, c109, width, color='Orange',align='edge', label='109') # Add some text for labels, title and custom x-axis tick labels Etc.#ax.set_title ('Scores by group and gender') plt.xticks (ind, (' Class 1', 'Class 2', 'Class 3', 'Class 4', 'Class 5', 'Class 6', 'Class 7', 'Class 8', 'Class 9', 'Class 10', 'Class 11', 'Class 12') ax.legend (loc='upper center') plt.show () fig.savefig ()

Upright

% matplotlib notebookimport matplotlib.pyplot as pltimport pandas as pdimport numpy as npimport seaborn as snsimport matplotlib# specifies the default font matplotlib.rcParams ['font.sans-serif'] = [' FangSong'] matplotlib.rcParams ['font.family'] =' sans-serif'# solves the minus sign'- 'the problem displayed as a square matplotlib.rcParams [' axes.unicode_minus'] = Falsedata = [8, 21, 3, 1, 1, 0,1], [7, 21, 3, 1, 1], [7, 21, 4, 5, 5, 5, 2, 0, 1] [3,24,10,1,0,0,1,0,1], [7,25,3,3,1,0,0,0,1], [10,16,9,2,3,1,0,0,0], [4,17,8,3,3,1,1,2,0], [11,11,12,3,1,0,0,1,1], [8,24,2,3,2,0,0,1,1], [11,24,4,6,0,1,0,0,0], [10,25,5,5,0,1,0,1,0], [8,36,2,1,0,0,0 ],] df = pd.DataFrame (data, index= ['Class 1', 'Class 2', 'Class 3', 'Class 4', 'Class 5', 'Class 6', 'Class 7', 'Class 8', 'Class 9', 'Class 10', 'Class 11', 'Class 12'), columns=pd.Index (['101', '102', '102', '103', '103', '104', '104') )) df.plot (kind='barh',figsize= (5) 8) plt.show () # fig.savefig ('. / test2.jpg')

At this point, the study on "how to draw a bar chart 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!

Tags: Bars learning data fonts more questions help practical ugly next between differences articles squares methods features theory knowledge articles websites Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS NVidia Xiaomi Linux MySQL