教育行業(yè)A股IPO第一股(股票代碼 003032)

全國咨詢/投訴熱線:400-618-4000

Python培訓(xùn)實(shí)例:汽車速度與制動距離的關(guān)系(添加網(wǎng)格)

更新時間:2022年03月22日11時48分 來源:傳智教育 瀏覽次數(shù):

  在之前的汽車速度與制動距離關(guān)系的散點(diǎn)圖中,很多圓點(diǎn)因距離坐標(biāo)軸較遠(yuǎn)而無法準(zhǔn)確地看出數(shù)值。因此,本實(shí)例將在散點(diǎn)圖中顯示網(wǎng)格,并調(diào)整坐標(biāo)軸的刻度,具體代碼如下。

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
x_speed = np.arange(10, 210, 10)
y_distance = np.array([0.5, 2.0, 4.4, 7.9, 12.3,
                       17.7, 24.1, 31.5, 39.9, 49.2,
                       59.5, 70.8, 83.1, 96.4, 110.7,
                       126.0, 142.2, 159.4, 177.6, 196.8])
plt.scatter(x_speed, y_distance, s=50, alpha=0.9, linewidths=0.3)
# 設(shè)置x軸的標(biāo)簽、刻度標(biāo)簽
plt.xlabel('速度(km/h)')
plt.ylabel('制動距離(m)')
plt.xticks(x_speed)
# 顯示網(wǎng)格
plt.grid(b=True, linewidth=0.3)
plt.show()

  運(yùn)行程序,效果如圖3-10所示。

汽車速度與制動距離關(guān)系的散點(diǎn)圖

  圖3-10 汽車速度與制動距離關(guān)系的散點(diǎn)圖——添加網(wǎng)格

  與圖2-19相比,圖3-10的散點(diǎn)圖增加了坐標(biāo)軸標(biāo)簽、淺灰色的網(wǎng)格,有助于用戶大致了解各數(shù)據(jù)點(diǎn)對應(yīng)的數(shù)值。

0 分享到:
和我們在線交談!