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

Posted 盼小辉丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了仅需10道题轻松掌握Matplotlib图形处理 | Python技能树征题相关的知识,希望对你有一定的参考价值。

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[仅需6道题轻松掌握Python时间和日期处理 | Python技能树征题

仅需10道题轻松掌握Python字符串方法 | Python技能树征题

仅需6道题轻松掌握Python异常捕获 | Python技能树征题

仅需6道题轻松掌握SciPy空间计算基础 | Python技能树征题

仅需5道题轻松掌握Python命令行相关标准库 | Python技能树征题

仅需8道题轻松掌握Python Web应用开发 | Python技能树征题