python 移動坐標軸到圖中央


1、簡單繪制 sin(x)

import mglearn import matplotlib.pyplot as plt mglearn.plots.plot_knn_regression(n_neighbors=2) plt.show()

 

 2、隱藏上邊和右邊的兩條軸線

import numpy as np import matplotlib.pyplot as plt x = np.linspace(-np.pi , np.pi) y = np.sin(x) plt.plot(x, y) ax = plt.gca() ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') plt.legend(['sin(x)']) plt.show()

 

 3、將坐標軸移至中心

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-np.pi , np.pi)
y = np.sin(x)

plt.plot(x, y)

ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))

plt.xlim(-4, 4)
plt.ylim(-1.5, 1.5)

plt.legend(['sin(x)'])

plt.show()

 

 

。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM