In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to achieve line chart, bar chart and pie chart in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Line chart
Code
Import numpy as npimport matplotlib.pyplot as plt# x-axis scale label x_ticks = ['a', 'baked,' cased, 'dashed,' eBay,'f'] # x-axis range (0,1,..., len (x_ticks)-1) x = np.arange (len (x_ticks)) # 1st broken line data Y1 = [5,3,2,4,1,6] # 2nd broken line data y2 = [3,1,6,5 2, 4] # set canvas size plt.figure (figsize= (10,6)) # draw the first broken line Parameters can be understood by name, and you can customize the style of data points, and so on. Plt.plot (x, y1, color='#FF0000', label='label1', linewidth=3.0) # draw the second broken line plt.plot (x, y2, color='#00FF00', label='label2', linewidth=3.0) # add a value to the first broken line data point, the first two parameters are coordinates, the third is numerical, ha and va are horizontal and vertical positions (relative values of data points). For a, b in zip (x, y1): plt.text (a, b,'% dashed% b, ha='center', va= 'bottom', fontsize=18) # add the values for a, b in zip (x, y2): plt.text (a, b,'% dashed% b, ha='center', va= 'bottom', fontsize=18) # to the data point of the second broken line, and the parameters indicate that a straight line will be drawn at the point of the second broken line. Plt.hlines (3,0, len (x)-1, colors = "# 000000", linestyles = "dashed") # add x-axis and y-axis scale labels plt.xticks ([r for r in x], x_ticks, fontsize=18, rotation=20) plt.yticks (fontsize=18) # add x-axis and y-axis labels plt.xlabel (ubiquitous labelings, fontsize=18) plt.ylabel (ubiquitous labelings, fontsize=18) # title plt.title Fontsize=18) # Legend plt.legend (fontsize=18) # Save picture plt.savefig ('. / figure.pdf', bbox_inches='tight') # display picture plt.show ()
Effect.
Bar chart
Code
Import numpy as npimport matplotlib.pyplot as plt# x-axis scale label x_ticks = ['a', 'baked,' cased, 'dashed,' eyed,'f'] # width of the column barWidth = 0.2 the x-axis range of the first column (the midpoint of each column) (0,1,. Len (x_ticks)-1) x1 = np.arange (len (x_ticks)) # x axis range of the second column (midpoint of each column) x2 = [x + barWidth for x in x1] # first column data y1 = [5,3,2,4,1,6] # second column data y2 = [3,1,6,5,2,4] # set canvas size plt.figure (figsize= (10,6)) # draw the first column plt.bar (x1) Y1, color='#FF0000', width=barWidth, label='label1') # draw the second column plt.bar (x2, y2, color='#00FF00', width=barWidth, label='label2') # add a number to the first column data point The first two parameters are coordinates, the third is numeric, and ha and va are horizontal and vertical positions (relative values of data points). For a, b in zip (x1, y1): plt.text (a, b,'% dashed% b, ha='center', va= 'bottom', fontsize=18) # add the values for a, b in zip (x2, y2): plt.text (a, b,'% dashed% b, ha='center', va= 'bottom', fontsize=18) # draw a horizontal line plt.hlines (3,0, len (x_ticks)-1+barWidth, colors = "# 000000") Linestyles = "dashed") # add x-axis and y-axis scale labels plt.xticks ([r + barWidth/2 for r in x1], x_ticks, fontsize=18) plt.yticks (fontsize=18) # add x-axis and y-axis labels plt.xlabel (upright xaxis labelings, fontsize=18) plt.ylabel (upright titles labelings, fontsize=18) # title plt.title (upright titles, fontsize=18) # Legend plt.legend (fontsize=18) # Save picture plt.savefig ('. / figure.pdf' Bbox_inches='tight') # display picture plt.show ()
Effect.
Pie chart
Code
Import numpy as npimport matplotlib.pyplot as plt# set canvas size plt.figure (figsize= (10, 10)) # set the label of each area labels = ['asides,' breadths, 'cages,' dwells,'e'] # set the distance of each area from the center of the circle Here a region protrudes a little bit explode= [0.05,0.01,0.01,0.01,0.01] # set the value of each area values = [1,5,2,4,3] # set the color of each area colors = ['# F5DEB3regions,'# 87CEFAregions,'# FFB6C1','# 90EE90colors,'# D3D3D3D3'] _, l_text, p_text = plt.pie (values, explode=explode, labels=labels, autopct='%1.1f%%') Colors=colors) # set label font size for t in l_text: t.set_size (18) # set numeric font size for t in p_text: t.set_size (18) # title plt.title (upped title, fontsize=18) # Legend plt.legend (fontsize=18) # Save picture plt.savefig ('. / figure.pdf' Bbox_inches='tight') # display picture plt.show () "how to realize line chart, bar chart and pie chart by Python" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.