You can also vibrato + me, directly search for " Aiwen programming ", welcome everyone to pay attention.
learning target
- Matplotlib concepts and uses
- import Matplotlib package
- set style
- Three ways to draw a graph
- keep graph to local file
what is data visualization
If you compare text data with chart data, the human thinking mode is more suitable for understanding the latter, because chart data is more intuitive and visualized. This method of using charts to represent data is called data visualization.
The figure below shows five commonly used data visualization charts
Data visualization mainly has the following application scenarios:
- Enterprise field: Use intuitive and diverse charts to display data to provide support for enterprise decision-making;
- Stock trend forecasting: Provide stockholders with more reasonable suggestions through the analysis of stock price data;
- Supermarket product sales: conduct data analysis on customer groups and purchased products, prompting supermarkets to formulate better sales strategies;
- Forecasting sales: analyzing the factors affecting product sales can predict the sales trend of products.
what is matplotlib
Matplotlib is a Python package for data visualization that supports cross-platform operation.
Matplotlib was written by John D. Hunter in 2002. In 2003, Matplotlib released the first version and joined the BSD Open Source Software Organization.
Matplotlib also supports embedding in IPython shell, Jupyter notebooks, web application servers in the form of scripts (just a few lines of code to generate plots, histograms, bar charts, error plots, scatter plots, etc.).
matplotlib download and install
Matplotlib is a third-party plotting library for Python that is very similar to MATLAB. Before using the Matplotlib package, it needs to be installed. Introduce several installation methods of Matplotlib.
MATLAB is a commercial software mainly used in data analysis, image processing, computer vision and other fields.
matplotlib package two ways
- pip install matplotlib
- conda install matplotlib
摘自:Matplotlib documentation — Matplotlib 3.6.2 documentation
import Matplotlib package
view version
set style
We'll use the plt.style directive to choose the appropriate aesthetic for our icon. Here we will set the classic style to ensure that the plots we create use the classic matplotlib style:
The first matplotlib plotting program
The Matplotlib icon is displayed in three ways: script, ipython terminal, and jupyter-notebook.
Run as script
If you are using matplotlib in scripts, the function plt.show() is your friend. show() starts an event loop, finds all currently active graphics objects, and opens one or more interactive windows that display graphics.
Drawing in IPython shell mode
It is very convenient to use matplotlib interactively in the ipython shell.
Jupyter notebook drawing
jupter-notebook is a browser-based interactive data analysis tool that can combine code, graphics, HTML elements, etc. into a single executable document.
Matplotlib figure saving
Save a picture, done by savefig() command (can keep different formats)
fig.savefig('images/my_figure.png')
fig.savefig('images/my_figure.pdf')
fig.savefig('images/my_figure.jpg')
For example: the pictures we save are as follows
In savefig(), the file format is inferred from the extension of the given filename. A list of file types supported by the system can be found by using the following methods of the Figure Canvas object:
Graphics further optimized (two graphics are displayed separately)
More case study references
https://matplotlib.org/cheatsheets/_images/handout-beginner.png