matplotlib以(0,0)為原點(diǎn)畫折線圖和legend方法

效果展示

效果展示
import matplotlib.pyplot as plt
import numpy as np

#figure就是一個(gè)大窗口
x = np.linspace(-3,3,50)
y1 = 2*x+1
y2 = x**2

plt.figure()
plt.plot(x,y2)
#figure plot第二條線,紅色虛線
plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')
#x limit和y limit
plt.xlim((-1,2))
plt.ylim((-2,3))
plt.xlabel('I am x')
plt.ylabel('I am y')
#設(shè)置坐標(biāo)軸的單位小標(biāo)
# new_ticks = np.linspace(-1,2,5)
# plt.xticks(new_ticks)
#坐標(biāo)軸小標(biāo)換成文字
plt.yticks([-2,-1.8,-1,1.23,3],['really bad','bad','normal','good','really good'])

#gca = 'get current axis'
ax = plt.gca()
#把上面和右面的軸設(shè)置為消失
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
#設(shè)置下面的線為x軸,左邊的線為y軸
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
#x軸的position放在y軸的0點(diǎn)上
ax.spines['bottom'].set_position(('data',-0))# outward,axes
ax.spines['left'].set_position(('data',0))
plt.legend(loc='lower right')
plt.show()

legend() loc屬性取值

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容