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

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

Python大數(shù)據(jù)培訓(xùn):移動軸脊的位置

更新時間:2022年04月26日10時36分 來源:傳智教育 瀏覽次數(shù):

  Python培訓(xùn)中我們經(jīng)常會講的移動軸脊的位置,在matplotlib中,Spine類提供了一個設(shè)置軸脊位置的set_position()方法。set_position()方法的語法格式如下所示:

set_position(self, position)

  該方法的position參數(shù)表示軸脊的位置,它需要接收一個包含兩個元素的元組(position_type,amount),其中元素position_type代表位置的類型,元素amount代表位置。position_type支持以下任一取值。

  .'outward':表示將軸脊置于移出數(shù)據(jù)區(qū)域指定點數(shù)的位置。

  .'axes':表示將軸脊置于指定的坐標(biāo)系中(0.0~1.0)。

  .'data':表示將軸脊置于指定的數(shù)據(jù)坐標(biāo)的位置。

  此外,position參數(shù)還支持以下兩個特殊的軸脊位置:

  .'center':值為('axes',0.5)。

  .'zero':值為('data',0.0)。

  例如,將畫布中軸脊的位置移動到中心位置,具體代碼如下。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpathes
xy = np.array([0.5,0.5])
polygon = mpathes.RegularPolygon(xy, 5, 0.2,color='y')
ax = plt.axes((0.3, 0.3, 0.5, 0.5))
ax.add_patch(polygon)
# 依次隱藏上軸脊和右軸脊
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
# 移動軸脊的位置
ax.spines['left'].set_position(('data', 0.5))
ax.spines['bottom'].set_position(('data', 0.5))
plt.show()

  運行程序,效果如圖6-9所示。

  圖6-9 移動軸脊至畫布的中心位置

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