Matplotlib data visualization (1)

Table of contents

1. Introduction to Matplotlib

2. Matplotlib drawing foundation

2.1 Create canvas and subgraph

2.2 Add canvas properties 

 2.3 Save and display drawing


1. Introduction to Matplotlib

Matplotlib is a Python library for drawing data visualization charts. It provides a wide range of functions and flexibility to create various types of charts, including line charts, scatter charts, histograms, pie charts, contour charts and 3D graphs, etc.

Matplotlib's design is inspired by MATLAB, so its usage is similar to the plotting functions in MATLAB. It is widely used in fields such as science, engineering, statistics, and data analysis, becoming one of the most commonly used data visualization tools in Python.

The core component of Matplotlib is an object-oriented plotting library. You can control the appearance and layout of a figure by creating a Figure object and one or more Axes objects. The Figure object represents the entire graphics window or canvas, while the Axes object represents the actual drawing area. Various methods and functions can be used to set properties of graphs, add legends, labels, titles, customize axes, colors, line styles, and more.

Matplotlib also supports the use of libraries such as Numpy and Pandas for data manipulation and processing, and can be seamlessly integrated with interactive environments such as Jupyter Notebook for convenient and fast data visualization and analysis.

2. Matplotlib drawing foundation

2.1 Create canvas and subgraph

  • plt.figure: create a blank canvas, you can specify the canvas size
  • figure.add_subplot: Create and select a subplot, you can specify the number of rows and columns of the subplot and the number of the selected picture
  • fig,axes=plt.subplots(m,n): generate subplots with m rows and n columns

Draw subplots :

import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax1.plot([1.5,2,3.5,-1,1.6]) 

Output result:

 Create a sequence of subplots and adjust the spacing around the subplots:

import matplotlib.pyplot as plt
import numpy as np
fig,axes = plt.subplots(2,2,sharex = True,sharey = True)
for i in range(2):
     for j in range(2):
            axes[i,j].hist(np.random.randn(500),bins =50,color='k',alpha= 0.5)
plt.subplots_adjust(wspace=0,hspace=0)

Output result:

2.2 Add canvas properties 

  • plt.title: add figure title
  • plt.xlabel: add the X-axis name in the current graph
  • plt.ylabel: Add the Y-axis name in the current graph
  • plt.xlim: Specify the X-axis range of the current graph
  • plt.ylim: Specify the Y-axis range of the current graph
  • plt.xticks: Specify the number and value of the X-axis scale
  • plt.yticks: Specify the number and value of the Y-axis scale
  • plt.legend: Specify the legend of the current graph, you can specify the size, position and label of the legend

Example:

import matplotlib.pyplot as plt
import numpy as np
data = np.arange(0,np.pi*2,0.01)
fig1 = plt.figure(figsize = (8,4),dpi = 90)  #确定画布大小
ax1 = fig1.add_subplot(1,2,1) #绘制第1幅子图
plt.title('lines example')
plt.xlabel('X')
plt.ylabel('Y')
plt.xlim(0,1)
plt.ylim(0,1)
plt.xticks([0,0.2,0.4,0.6,0.8,1])
plt.yticks([0,0.2,0.4,0.6,0.8,1])
plt.plot(data,data**2)
plt.plot(data,data**3)
plt.legend(['y = x^2','y = x^3'])
ax1 = fig1.add_subplot(1,2,2) #绘制第2幅子图
plt.title('sin/cos')
plt.xlabel('X')
plt.ylabel('Y')
plt.xlim(0,np.pi*2)
plt.ylim(-1,1)
plt.xticks([0,np.pi/2,np.pi,np.pi*3/2,np.pi*2])
plt.yticks([-1,-0.5,0,0.5,1])
plt.plot(data,np.sin(data))
plt.plot(data,np.cos(data))
plt.legend(['sin','cos'])
plt.show()

result:

 2.3 Save and display drawing

Functions for plot display and saving:

  • plt.savefig: Save the drawn picture
  • plt.show: display graphics

savefig options and their descriptions:

  • fname: a string containing a file path or a python file-like object
  • dpi: resolution, the default is 100
  • facecolor, edgecolor: the background color of the graphics outside the subgraph
  • format: file format

Guess you like

Origin blog.csdn.net/m0_64087341/article/details/132319177