# Only 10 questions are needed to easily master Matplotlib graphics processing | Python skill tree

Hope Xiaohui 2021-10-29 03:00:53
questions needed easily master matplotlib

## 0. Preface

Matplotlib yes Python Drawing library of , It offers a complete set and matlab Similar orders API, It can generate exquisite graphics of publishing quality level ,Matplotlib Make drawing very simple , We'll pass 10 Avenue Python Programming questions to master the use Matplotlib Library for graphic drawing ！

## 1. The first 1 topic ： Drawing of curve

Knowledge point description ： Draw a curve .
Problem description ： Draw functions in the same picture y = x 2 y=x^2 , y = l o g e x y=log_ex as well as y = s i n ( x ) y=sin(x) , Please choose the correct answer from the following options ：
A.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.1, 2 * np.pi, 100)
y_1 = np.square(x)
y_2 = np.log(x)
y_3 = np.sin(x)
fig = plt.figure()
plt.plot(x,y_1)
fig = plt.figure()
plt.plot(x,y_2)
fig = plt.figure()
plt.plot(x,y_3)
plt.show()


B.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.1, 2 * np.pi, 100)
y_1 = np.square(x)
y_2 = np.log(x)
y_3 = np.sin(x)
fig = plt.figure()
plt.plot(x,y_1)
plt.plot(x,y_2)
plt.plot(x,y_3)
plt.show()


C.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.1, 2 * np.pi, 100)
y_1 = np.square(x)
y_2 = np.log(x)
y_3 = np.sin(x)
plt.plot(x,y_1, y_2, y_3)
plt.show()


D.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.1, 2 * np.pi, 100)
y_1 = np.square(x)
y_2 = np.log(x)
y_3 = np.sin(x)
fig = plt.figure()
plt.plot(x,y_1, y_2, y_3)
plt.show()


right key ： B

## 2. The first 2 topic ： The drawing of scatter diagram

Knowledge point description ： Draw a scatter plot .
Problem description ： Draw function y = s i n ( x ) y=sin(x) The points on the , Please choose the correct answer from the following options ：
A.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.1, 2 * np.pi, 50)
y = np.sin(x)
fig = plt.figure()
plt.plot(x, y)
plt.show()


B.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.1, 2 * np.pi, 50)
y = np.sin(x)
fig = plt.figure()
plt.barh(x, y)
plt.show()


C.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.1, 2 * np.pi, 50)
y = np.sin(x)
fig = plt.figure()
plt.bar(x, y)
plt.show()


D.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.1, 2 * np.pi, 50)
y = np.sin(x)
fig = plt.figure()
plt.scatter(x, y)
plt.show()


right key ： D

## 3. The first 3 topic ： Drawing a bar chart

Knowledge point description ： Draw a bar graph .
Problem description ： Draw multiple sets of bar charts , Compare the sales volume of the corresponding quarter in different years , Please choose the right one from the following ：
A.

import numpy as np
import matplotlib.pyplot as plt
data = [[10., 20., 30., 20.],[40., 25., 53., 18.],[6., 22., 52., 19.]]
x = np.arange(4)
colors = ['r', 'g', 'b']
for i in range(len(data)):
plt.bar(x + i * 0.25, data[:i], color = colors[i], width = 0.25)
plt.show()


B.

import numpy as np
import matplotlib.pyplot as plt
data = [[10., 20., 30., 20.],[40., 25., 53., 18.],[6., 22., 52., 19.]]
x = np.arange(4)
colors = ['r', 'g', 'b']
for i in range(len(data)):
plt.plot(x + i * 0.25, data[:i], color = colors[i], width = 0.25)
plt.show()


C.

import numpy as np
import matplotlib.pyplot as plt
data = [[10., 20., 30., 20.],[40., 25., 53., 18.],[6., 22., 52., 19.]]
x = np.arange(4)
colors = ['r', 'g', 'b']
for i in range(len(data)):
plt.bar(x + i * 0.25, data[i], color = colors[i], width = 0.25)
plt.show()


D.

import numpy as np
import matplotlib.pyplot as plt
data = [[10., 20., 30., 20.],[40., 25., 53., 18.],[6., 22., 52., 19.]]
x = np.arange(4)
colors = ['r', 'g', 'b']
for i in range(len(data)):
plt.plot(x + i * 0.25, data[i], color = colors[i], width = 0.25)
plt.show()


right key ： C

## 4. The first 4 topic ： The drawing of pie chart

Knowledge point description ： Use a pie chart to compare the relative relationship between quantities .
Problem description ： Draw the pie chart , Comparison list [10, 15, 30, 20] The relative relationship between quantities , Please choose the right one from the following ：
A.

import matplotlib.pyplot as plt
data = [10, 15, 30, 20]
sum_data = sum(data)
plt.pie(data / sum_data)
plt.show()


B.

import matplotlib.pyplot as plt
data = [10, 15, 30, 20]
plt.pie(sum(data))
plt.show()


C.

import matplotlib.pyplot as plt
data = [10, 15, 30, 20]
plt.pie(range(len(data)), data)
plt.show()


D.

import matplotlib.pyplot as plt
data = [10, 15, 30, 20]
plt.pie(data)
plt.show()


right key ： D

## 5. The first 5 topic ： Histogram drawing

Knowledge point description ： A histogram is used to represent the probability distribution .
Problem description ： Draw the histogram according to the constructed array , Please choose the correct answer from the following options ：
A.

import numpy as np
import matplotlib.pyplot as plt
x = np.random.randn(1024)
plt.hist(x, bins = 20)
plt.show()


B.

import numpy as np
import matplotlib.pyplot as plt
x = np.random.randn(1024)
plt.hist(x, bins=x.shape)
plt.show()


C.

import numpy as np
import matplotlib.pyplot as plt
x = np.random.randn(1024)
plt.hist(x.shape, x)
plt.show()


D.

import numpy as np
import matplotlib.pyplot as plt
x = np.random.randn(1024)
plt.hist(x, x.shape)
plt.show()


right key ： A

## 6. The first 6 topic ： Add the title

Knowledge point description ： Add a title to the drawing .
Problem description ： Add a Chinese title to the drawing , Please choose the correct answer from the following options ：
A.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-4, 4, 10005)
y = 5 * (x + 4.2) * (x + 4.) * (x - 2.5)
plt.title(' curve ')
plt.plot(x, y, c = 'm')
plt.show()


B.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-4, 4, 10005)
y = 5 * (x + 4.2) * (x + 4.) * (x - 2.5)
plt.title(' curve ')
plt.plot(x, y, c = 'm')
plt.rcParams['font.sans-serif'] = ['SimSun']
plt.show()


C.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-4, 4, 10005)
y = 5 * (x + 4.2) * (x + 4.) * (x - 2.5)
plt.plot(x, y, c = 'm', title=' curve ')
plt.show()


D.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-4, 4, 10005)
y = 5 * (x + 4.2) * (x + 4.) * (x - 2.5)
plt.plot(x, y, c = 'm', title=' curve ')
plt.rcParams['font.sans-serif'] = ['SimSun']
plt.show()


right key ： B

## 7. The first 7 topic ： Label the axes

Knowledge point description ： Add appropriate description labels for the coordinate axes of the drawing to help users understand the meaning of the drawing .
Problem description ： A function is known to describe accelerated motion , Please draw a graph to show the relationship between time and distance ：
A.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 8, 1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
plt.xtitle('Time')
plt.ytitle('distance')
plt.plot(x, y, c = 'c')
plt.show()


B.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 8, 1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
plt.plot(x, y, c = 'c', xlabel = 'Time', ylable = 'distance')
plt.show()


C.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 8, 1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
plt.plot(x, y, c = 'c', xtitle = 'Time', ytitle = 'distance')
plt.show()


D.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 8, 1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
plt.xlabel('Time')
plt.ylabel('distance')
plt.plot(x, y, c = 'c')
plt.show()


right key ：D

## 8. The first 8 topic ： Add a text description to the drawing

Knowledge point description ： Add description text to the drawing , Highlight the importance of points or lines in the diagram .
Problem description ： Use text to explicitly mark the midpoint of the function image , Please choose the correct answer from the following options ：
A.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 8, 1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
x_mid = x[0]
y_mid = y[0]
plt.scatter(x_mid, y_mid)
plt.text(x_mid, y_mid, 'mid')
plt.plot(x, y, c = 'c')
plt.show()


B.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 8, 1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
x_mid = (x[-1] - x[0]) / 2
y_mid = 2.0 * ((x[-1] - x[0]) / 2) + 0.5 * 5 * ((x[-1] - x[0]) / 2) ** 2
plt.scatter(x_mid, y_mid)
plt.text('mid')
plt.plot(x, y, c = 'c')
plt.show()


C.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 8, 1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
x_mid = x[-1]
y_mid = y[-1]
plt.scatter(x_mid, y_mid)
plt.text(x_mid, y_mid, 'mid')
plt.plot(x, y, c = 'c')
plt.show()


D.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 8, 1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
x_mid = (x[-1] - x[0]) / 2
y_mid = 2.0 * ((x[-1] - x[0]) / 2) + 0.5 * 5 * ((x[-1] - x[0]) / 2) ** 2
plt.scatter(x_mid, y_mid)
plt.text(x_mid, y_mid, 'mid')
plt.plot(x, y, c = 'c')
plt.show()


right key ：D

## 9. The first 9 topic ： Add arrows to the drawing

Knowledge point description ： Use arrows to indicate specific parts of the drawing .
Problem description ： Use arrows to explicitly mark the midpoint of the function image , Please choose the correct answer from the following options ：
A.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 8, 1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
x_mid = (x[-1] - x[0]) / 2
y_mid = 2.0 * ((x[-1] - x[0]) / 2) + 0.5 * 5 * ((x[-1] - x[0]) / 2) ** 2
plt.annotate('mid',
ha = 'center', va = 'bottom',
xytext = (5, 30.),
xy = (x_mid, y_mid),
arrowprops = {
'facecolor' : 'black', 'shrink' : 0.05 })
plt.plot(x, y, c = 'c')
plt.show()


B.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 8, 1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
x_mid = (x[-1] - x[0]) / 2
y_mid = 2.0 * ((x[-1] - x[0]) / 2) + 0.5 * 5 * ((x[-1] - x[0]) / 2) ** 2
plt.annotate('mid',
ha = 'center', va = 'bottom',
xytext = (5, 30.),
x = x_mid,
y = y_mid,
arrowprops = {
'facecolor' : 'black', 'shrink' : 0.05 })
plt.plot(x, y, c = 'c')
plt.show()


C.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 8, 1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
x_mid = (x[-1] - x[0]) / 2
y_mid = 2.0 * ((x[-1] - x[0]) / 2) + 0.5 * 5 * ((x[-1] - x[0]) / 2) ** 2
plt.annotate('mid',
ha = 'center', va = 'bottom',
xtext = 5,
ytext = 30.,
xy = (x_mid, y_mid),
arrowprops = {
'facecolor' : 'black', 'shrink' : 0.05 })
plt.plot(x, y, c = 'c')
plt.show()


D.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 8, 1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
x_mid = (x[-1] - x[0]) / 2
y_mid = 2.0 * ((x[-1] - x[0]) / 2) + 0.5 * 5 * ((x[-1] - x[0]) / 2) ** 2
plt.annotate('mid',
ha = 'center', va = 'bottom',
xtext = 5,
ytext = 30.,
xy = (x_mid, y_mid),
arrowprops = {
'facecolor' : 'black', 'shrink' : 0.05 })
plt.plot(x, y, c = 'c')
plt.show()


right key ：A

## 10. The first 10 topic ： Add a legend to the drawing

Knowledge point description ： Add corresponding legends for curves and points in the drawing , To make an accurate distinction .
Problem description ： A graph contains multiple curves and scatter points , Add legends to them , Please choose the correct answer from the following options ：
A.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 6, 1024)
data = np.random.standard_normal((150, 2))
y_1 = np.sin(x)
y_2 = np.cos(x)
plt.plot(x, y_1, c = 'm', lw = 3., text = 'sin(x)')
plt.plot(x, y_2, c = 'c', lw = 3., ls = '--', text = 'cos(x)')
plt.scatter(data[:,0], data[:, 1], c = 'y', text = 'random')
plt.legend()
plt.show()


B.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 6, 1024)
data = np.random.standard_normal((150, 2))
print(data.size)
y_1 = np.sin(x)
y_2 = np.cos(x)
plt.plot(x, y_1, c = 'm', lw = 3., title = 'sin(x)')
plt.plot(x, y_2, c = 'c', lw = 3., ls = '--', title = 'cos(x)')
plt.scatter(data[:,0], data[:,1], c = 'y', title = 'random')
plt.legend()
plt.show()


C.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 6, 1024)
data = np.random.standard_normal((150, 2))
y_1 = np.sin(x)
y_2 = np.cos(x)
plt.plot(x, y_1, c = 'm', lw = 3., label = 'sin(x)')
plt.plot(x, y_2, c = 'c', lw = 3., ls = '--', label = 'cos(x)')
plt.scatter(data[:, 0], data[:, 1], c = 'y', label = 'random')
plt.legend()
plt.show()


D.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 6, 1024)
data = np.random.standard_normal((150, 2))
y_1 = np.sin(x)
y_2 = np.cos(x)
plt.plot(x, y_1, c = 'm', lw = 3., legend = 'sin(x)')
plt.plot(x, y_2, c = 'c', lw = 3., ls = '--', legend = 'cos(x)')
plt.scatter(data[:, 0], data[:, 1], c = 'y', legend = 'random')
plt.legend()
plt.show()


right key ：C

## Question code address

https://codechina.csdn.net/LOVEmy134611/python_problem

https://pythonmana.com/2021/10/20211013145815502j.html