Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw line chart and bar chart by python

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how to draw line charts and bar charts in python. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Line chart

When the values of import matplotlib.pyplot as plt#x axes are different # x1 = [0PEM 0.1ret 0.3Regy 0.7Regy 0.8WE 0.9] # y1 = [0.71500.147Met 0.7088] # y1 = [0.996WH 0.6942pr 0.5599] # x2 = [0Mie 0.1Me 0.2pr 0.5pr 0.6pr 0.8pr 0.9re1] # y2= [0.71500.146pr 0.696pr 0.696recorder 0.3344] # x3 = [0prima 0.1pr 0.2pr 0.2pr 0.3rect 0.4ther 0.7ther 0.6ther 0.4ther 0.7ther 0.7cet 0.8cet 0.981] # y3 = [0.7150pr 0.7147e 0.7147pr 0.147e .7068, 0.7016, 0.6283, 0.5889, 0.5155, 0.4992, 0.4728, 0.3909, 0.3310) # when the x-axis has the same value, x = [1. ] y1 = [57pje 74pyrrt6] y1 = [57pje 74pyrm 69pyrm 82pr 99pr 99] y2 = [44pr 57pr 48pr 55pr 56pr 79] y3 = [61pl 7pr 59pr 59pr 59pr 598pl 588pr 588J 588J 548pcm68j81] plt.title ('courier monthly quantity') # line chart title plt.rcParams ['font.sans-serif'] = [' SimHei'] # line chart when Chinese characters need to be displayed Add this line plt.xlabel ('month / month') # x-axis title plt.ylabel ('number of couriers') # y-axis title plt.plot (x, y1, marker='o', markersize=3) # draw a line chart Add data point shape and set point size plt.plot (x, y2, marker=' ^', markersize=3) # ^: point shape is triangular plt.plot (x, y3, marker='*', markersize=3) # star for a, b in zip (x, y1): plt.text (a, b, b, ha='center', va='bottom', fontsize=10) # set data label position and font size for a, b in zip (x, y2): plt.text (a B, b, ha='center', va='bottom', fontsize=10) for a, b in zip (x, y3): plt.text (a, b, b, ha='center', va='bottom', fontsize=10) plt.legend (['Post', 'Shun Feng', 'Yuantong']) # set the line name plt.show () # display the line chart

Results:

Single bar graph

When the import matplotlib.pyplot as plt# bar chart needs to be displayed in Chinese You need the following two lines of code plt.rcParams ['font.sans-serif'] = [' SimHei'] plt.rcParams ['axes.unicode_minus'] = Falsequarters = (' first section', 'second quarter', 'third quarter', 'fourth quarter') # x-axis courier_number = [310,382,256,402] # number of x-axis plt.bar (quarters, courier_number) # drawing # plt.barh (quarters) Courier_number) # to put a bar chart horizontally Use the function barhplt.title ('results of the survey on the number of express deliveries in four quarters') # bar chart title plt.show ()

Results:

Juxtaposed bar chart

When the import matplotlib.pyplot as pltimport numpy as np# bar chart needs to be displayed in Chinese You need the following two lines of code plt.rcParams ['font.sans-serif'] = [' SimHei'] plt.rcParams ['axes.unicode_minus'] = False# to enter statistics quarters = (' first section', 'second quarter', 'third quarter', 'fourth quarter') # x-axis courier_number_before = [310,382,256,402] courier_number_now = [320,420,388 432] bar_width = 0.3 # set bar width index_before = np.arange (len (quarters)) # Abscissa of the previous fourth quarter bar index_now = index_before + bar_width # now the Abscissa of the fourth quarter bar chart # use the bar function twice to draw two sets of bar charts plt.bar (index_before, height=courier_number_before, width=bar_width, color='b', label=') plt.bar (index_now, height=courier_number_now) Width=bar_width, color='g', label=' this year') plt.legend () # display legend plt.xticks (index_before + bar_width/2, quarters) # Let the Abscissa scale show the express volume for four quarters Index_before + bar_width/2 is the position of the horizontal axis scale plt.ylabel ('number of deliveries') # ordinate axis title plt.title ('results of the survey on the number of deliveries in the four quarters of last year') # graphic title plt.show ()

These are all the contents of the article "how to draw line charts and bars in python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report