仅需10道题轻松掌握Matplotlib图形处理 | Python技能树征题

仅需10道题轻松掌握Matplotlib图形处理 | Python技能树征题

    • 0. 前言
    • 1. 第 1 题:曲线图的绘制
    • 2. 第 2 题:散点图的绘制
    • 3. 第 3 题:条形图的绘制
    • 4. 第 4 题:饼图的绘制
    • 5. 第 5 题:直方图的绘制
    • 6. 第 6 题:添加标题
    • 7. 第 7 题:为坐标轴添加标签
    • 8. 第 8 题:在图形中添加文本说明
    • 9. 第 9 题:在图形中添加箭头
    • 10. 第 10 题:在图形中添加图例
    • 试题代码地址

0. 前言

Matplotlib 是 Python 的绘图库,它提供了一整套和 matlab 相似的命令 API,可以生成出版质量级别的精美图形,Matplotlib 使绘图变得非常简单,我们就通过 10Python 编程题来掌握使用 Matplotlib 库进行图形绘制吧!

1. 第 1 题:曲线图的绘制

知识点描述:绘制曲线图。
问题描述:在同一图片中绘制函数 y = x 2 y=x^2 y=x2 y = l o g e x y=log_ex y=logex以及 y = s i n ( x ) y=sin(x) y=sin(x),请从以下选项中选出你认为正确的答案:
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()

正确答案: B

2. 第 2 题:散点图的绘制

知识点描述:绘制散点图。
问题描述:绘制函数 y = s i n ( x ) y=sin(x) y=sin(x)上的点,请从以下选项中选出你认为正确的答案:
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()

正确答案: D

3. 第 3 题:条形图的绘制

知识点描述:绘制条形图。
问题描述:绘制多组条形图,比较不同年份相应季度的销量,请从以下选项中选出你认为正确的选项:
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()

正确答案: C

4. 第 4 题:饼图的绘制

知识点描述:使用饼图对比数量间的相对关系。
问题描述:绘制饼图,对比列表 [10, 15, 30, 20] 数量间的相对关系,请从以下选项中选出你认为正确的选项:
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()

正确答案: D

5. 第 5 题:直方图的绘制

知识点描述:使用直方图表示概率分布。
问题描述:根据构造数组绘制直方图,请从以下选项中选出你认为正确的答案:
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()

正确答案: A

6. 第 6 题:添加标题

知识点描述:在图形中添加标题。
问题描述:为所绘制的图形添加中文标题,请从以下选项中选出你认为正确的答案:
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('曲线')
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('曲线')
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='曲线')
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='曲线')
plt.rcParams['font.sans-serif'] = ['SimSun']
plt.show()

正确答案: B

7. 第 7 题:为坐标轴添加标签

知识点描述:为图形坐标轴的添加适当描述标签帮助用户理解图形所表达的含义。
问题描述:已知一函数用于描述加速运动,请绘制一图形表示时间与距离间关系:
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()

正确答案:D

8. 第 8 题:在图形中添加文本说明

知识点描述:在图形中添加说明文本,凸显图中点或线的重要性。
问题描述:使用文本显式标记函数图像的中点,请从以下选项中选出你认为正确的答案:
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()

正确答案:D

9. 第 9 题:在图形中添加箭头

知识点描述:使用箭头说明图形中的特定部分。
问题描述:使用箭头显式标记函数图像的中点,请从以下选项中选出你认为正确的答案:
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()

正确答案:A

10. 第 10 题:在图形中添加图例

知识点描述:为图形中的曲线和点添加相应的图例,以进行准确的区分。
问题描述:一图形中包含多条曲线和散点,为它们添加图例,请从以下选项中选出你认为正确的答案:
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()

正确答案:C

试题代码地址

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

你可能感兴趣的