Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Matplotlib to draw Chart in Python

Shulou Source: shulou.com Published: 2022-06-01 23:43:53 09月14日 Update

This article mainly introduces the relevant knowledge of "how to draw charts with Matplotlib in Python". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to draw charts with Matplotlib in Python" can help you solve the problem.

preface

Matplotlib is a MATLAB-like drawing tool in Python, and if you are familiar with MATLAB, you can quickly become familiar with it.

Matplotlib provides an object-oriented drawing API that makes it easy to embed graphics in applications with Python GUI toolkits such as PyQt, WxPython, and Tkinter. It is also supported as scripts for Python, IPython Shell, Jupyter Notebook, and Web application servers.

Line Chart Drawing and Display #Show Weather for a Week #1. Create Canvas plt.figure (figsize =(20,8))#plt. figure (figsize =(), dpi =)#figsize: Specify the length and width of the image #dpi: Image sharpness #Return fig object #2. Draw image plt. plot ([1,2,3,4,5,6,7],[17,17,18,15,11,11,13], label ="hh ")#plt. plot (x, y, color =, line)#figure, axes = plt. subplots (nrows = 1, ncols = 2, figsize =(20,8), dpi = 80)#Show legend plt. legend (loc =" lower left ")#Add grid display plt. grid (True, linestyle ='-', alpha = 0.5)#3. Save images must precede show because show frees image resources #plt. savefig (" test.png ")#4. Show images plt.show ()

Draw math function image import numpy as np #1. Prepare x, y data x = np. linspace (-1,1,1000) y = 2 * x * x #2. Create canvas plt. figure (figsize =(20,8), dpi = 80)#3. Draw image plt. plot (x, y)#4. Display image plt.show ()

from Jupyterthemes import www.example.com (theme ='monokai ')#Select a drawing theme import matplotlib. pyplot as plt #The following code is required for individual environments % matplotlib inlineplt. figure () plt. plot ([1,0,9],[4,5,6]) plt.show ()

Scatter Plot Drawing #1. Prepare data x, y =[1,2,3,4,5,6,7],[17,17,18,15,11,11,13]#2. Create canvas plt. figure (figsize =(20, 8))#3. Draw image plt. scatter (x, y)#4. Display image plt.show ()

Draw histogram #1. Prepare data x, y =[1,2,3,4,5,6,7],[17,17,2,15,11,13]#2. Create canvas plt. figure (figsize =(20,8))#3. Draw image plt.bar (x, y, width = 0.5,color =['r ',' b','y',' g'])#4. Display image plt.show ()

Draw a histogram x =[1,2,3,4,5,6,17,17,18,15,11,45,12,54,23,45,6,12,87,51,11,13] plt. figure (figsize =(20,8), dpi = 80) distance = 2group_num = int ((max (x)-min (x))/distance) plt. hist (x, bins = group_num) plt.show ()

pie chart #1. Prepare data x, y =[1,2,3,4,5,6,7],['17',' 17','2',' 15','11',' 11','13']#2. Create canvas plt. figure (figsize =(20, 8))#3. Draw image plt. pie (x, labels = y, autopct ='%1.2f %%', colors =[' r','b',' y','g'])#x, y-axis scale equal length plt. axis ('equal ') plt. legend (loc ="lower left ")#4. Display image plt.show ()

That's all for "How to draw a chart with Matplotlib in Python". Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.

Tags: Images canvases data preparation charts knowledge drawings objects tools industries applications different practical at the same time code content functions scales prefaces legends Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Apple Linux Shulou Technology Microsoft