YouTip LogoYouTip

Matplotlib Pyplot

Pyplot is a sub-library of Matplotlib, providing a plotting API similar to MATLAB. Pyplot is a commonly used plotting module, which allows users to easily create 2D charts. Pyplot contains a series of related functions for plotting. Each function will make some modifications to the current image, such as: adding markers to the image, creating new images, creating new plotting areas in the image, etc. When using it, we can import the pyplot library using import and set an alias plt: import matplotlib.pyplot as plt This allows us to use plt to reference the methods of the Pyplot package. Here are some commonly used pyplot functions: * `plot()`: Used for drawing line charts and scatter plots * `scatter()`: Used for drawing scatter plots * `bar()`: Used for drawing vertical and horizontal bar charts * `hist()`: Used for drawing histograms * `pie()`: Used for drawing pie charts * `imshow()`: Used for drawing images * `subplots()`: Used for creating subplots In addition to these basic functions, pyplot provides many other functions, such as functions for setting chart properties, functions for adding text and annotations, functions for saving charts to files, etc. In the following example, we draw a line through two coordinates **(0,0)** to **(6,100)**: ## Example import matplotlib.pyplot as plt import numpy as np xpoints = np.array([0,6]) ypoints = np.array([0,100]) plt.plot(xpoints, ypoints) plt.show() The output is as follows: !(#) In the above example, we used the plot() function of Pyplot. The plot() function is the most basic function for drawing two-dimensional graphics. plot() is used for drawing, it can draw points and lines. The syntax is as follows: # Draw a single line plot(, y, , *, data=None, **kwargs)# Draw multiple lines plot(, y, , , y2, , ..., **kwargs) Parameter description: * **x, y:** Points or line nodes, x is the x-axis data, y is the y-axis data, data can be a list or array. * **fmt:** Optional, defines basic format (such as color, marker, and line style). * ****kwargs:** Optional, used on two-dimensional plots to set specified properties, such as labels, line width, etc. >>> plot(x, y)# Create a 2D line plot of data in y against corresponding values in x, using default style >>> plot(x, y,'bo')# Create a 2D line plot of data in y against corresponding values in x, using blue solid circles >>> plot(y)# x values are 0..N-1 >>> plot(y,'r+')# Use red + symbols **Color characters:** 'b' blue, 'm' magenta, 'g' green, 'y' yellow, 'r' red, 'k' black, 'w' white, 'c' cyan, '#008000' RGB color string. When multiple curves do not specify colors, they will automatically select different colors. **Line style parameters:** '‐' solid line, '‐‐' dashed line, '‐.' dash-dot line, ':' dotted line. **Marker characters:** '.' point marker, ',' pixel marker (extremely small point), 'o' solid circle marker, 'v' triangle down marker, '^' triangle up marker, '>' triangle right marker, '<' triangle left marker... etc. If we want to draw a line from coordinates (1, 3) to (8, 10), we need to pass two arrays [1, 8] and [3, 10] to the **plot** function: ## Example import matplotlib.pyplot as plt import numpy as np xpoints = np.array([1,8]) ypoints = np.array([3,10]) plt.plot(xpoints, ypoints) plt.show() The output of the above code is as follows: !(#) If we only want to draw two coordinate points instead of a line, we can use the o parameter, which represents a solid circle marker: ## Draw two points at coordinates (1, 3) and (8, 10) import matplotlib.pyplot as plt import numpy as np xpoints = np.array([1,8]) ypoints = np.array([3,10]) plt.plot(xpoints, ypoints,'o') plt.show() The output
← Matplotlib LineMatplotlib Tutorial β†’