In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Python matplotlib is how to draw, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.
1. Introduction of matpltlib library
matplotlib is an excellent Python data visualization third-party library
When drawing with matpltlib library, first introduce it, load pyplot inside, and name it plt, and then use plot function to draw
import matplotlib.pyplot as plt #plt is an alias for the imported module 2.pyplot Basic Icon Function Summary
3. Plot function drawing syntax rules
plot function parameters: plot([x],y,[format],**kwargs)
There are too many grammars, steal a few MOOC pictures and put them up ~
4. line chart
from matplotlib import pyplot as plt
#generated data #abscissa data from 2017 to 2022, the third parameter can control the step size, can write or not write x = range (2017, 2022)#y value corresponding to ordinate y1 = [49, 48, 45, 52, 50]y2 = [60, 62, 61, 65, 63]#generate graph plt.title ("LMY and her mother's weight")plt.xlabel('year')plt.ylabel('kg')plt.plot(x, y1, color='green', label='LMY')plt.plot(x, y2, color='purple', label='mother')plt.grid(alpha=0.5)plt.legend(loc='upper right')#Display graph plt.show()
4. scatter plot
from matplotlib import pyplot as pltimport numpy as np#generate data #abscissa data from 2017 to 2022, the third parameter can control the step size, can be written or not x = range (2017, 2022)# y corresponds to the ordinate value y1 = [49, 48, 45, 52, 50]y2 = [60, 62, 61, 65, 63]#Generate graphics plt.title ("LMY and her mother's weight")plt.xlabel('year')plt.ylabel('kg')#dot size area = np.pi*4**2plt.scatter (x, y1, s=area, c='yellow', alpha=1)plt.scatter(x, y2, s=area, c='blue', alpha=1)plt.legend()plt.yticks(())plt.show()
5. histogram
from matplotlib import pyplot as pltimport numpy as np#generate data #abscissa data from 2017 to 2022, the third parameter can control the step size, can write or not write x = [2017, 2018, 2019, 2020, 2021]# y corresponding to the ordinate value y1 = [49, 48, 45, 52, 50]y2 = [60, 62, 61, 65, 63]#Generate graphics plt.title ("LMY and her mother's weight")plt.ylabel ('frequency ')plt.xlabel ('kg')#Size of point plt.hist (y1, bottom=None, color='purple')plt.hist (y2, bottom=None, color='pink')plt.show()# n, bins, patches = plt.hist (arr, bins=50, normed=1, facecolor='green', alpha=0.75)'''arr: 1-D array to calculate histogram bins: Number of histogram bins, optional, default is 10normed: Normalize the resulting histogram vector, default is 0facecolor: histogram color alpha: transparency''
6. Bar charts
longitudinal
from matplotlib import pyplot as pltimport numpy as nparr = np.arange(2017, 2022)x = [49, 48, 45, 52, 50] #x-axis y = [2017, 2018, 2019, 2020, 2021]rect = plt.bar (arr, x, width=0.5)plt.title('LMY')plt.xlabel('weight') plt.ylabel('year') plt.legend()plt.show()
transverse
plurality of
from matplotlib import pyplot as pltimport numpy as nparr = np.arange(2017, 2022)x1 = [49, 48, 45, 52, 50] #x-axis x2 = [60, 62, 61, 65, 63]y = [2017, 2018, 2019, 2020, 2021]rects1 = plt.bar (arr, x1, 0.5, color='purple', label='LMY')rects2 = plt.bar(arr, x2, 0.5, color='yellow', label='Mother', alpha=0.3)plt.title("LMY and her mother's weight")plt.xlabel('weight')plt.ylabel('year')plt.legend()plt.show()
7. pie chart
from matplotlib import patches, pyplot as pltimport numpy as nplabel_list = ['49',' 48','45',' 52','50'] size = [20, 20, 20, 20]#color = [' red','pink',' blue','green',' purple'] explore = [0, 0, 0.15, 0, 0]patches, l_text, p_text = plt.pie(size, explode=explode, colors=color, labels=label_list, labeldistance=1.2, autopct="%1.2f%%", shadow=False, startangle=90, pctdistance=0.6)plt.axis('equal')plt.title("LMY's weight")plt.legend(loc='upper left')plt.show()
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.