python 可视化 二维坐标标注等等
python 可視化 二維坐標標注等等
基本畫圖操作:
import matplotlib.pyplot as plt import numpy as np x = np.linspace(-3,3,50) y1 = 2*x+1 y2 = x**2#x的平方 plt.figure() plt.plot(x,y1) #畫線 plt.scatter(x,y2) #畫點 plt.figure(num=333,figsize=(8,5))#圖333 plt.plot(x,y2) plt.show()
設置圖例:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3,3,50)
y1 = 2*x+1
y2 = x**2#x的平方
plt.figure()
plt.xlim((-1,2))
plt.ylim((-2,3))
plt.xlabel('I am x')
plt.ylabel('I am y')
new_ticks = np.linspace(-1,2,5)#5為5個單位
print(new_ticks)
plt.xticks(new_ticks)
#用r(正則表達)和$框起來可以轉換為計算機可以讀的字體
#加空格轉義為空格,加alpha能夠輸出 真正的alpha
plt.yticks([-2,-1.8,-1,1.22,3,],
[r'$really bad$',r'$bad alpha$','normal','good','really good'])
l1,=plt.plot(x,y2,label='up')#畫線
l2,=plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--',label='dowm')#‘--’為虛線
#loc可以為upper right等等
#要傳到handles要加,用了labels后就不用l1,l2本身的label
plt.legend(handles=[l1,l2,],labels=['aaa','bbb'],loc='best')
plt.show()
設置坐標軸位置:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3,3,50)
y1 = 2*x+1
y2 = x**2#x的平方
plt.figure()
plt.plot(x,y2)
plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')
plt.xlim((-1,2))
plt.ylim((-2,3))
plt.xlabel('I am x')
plt.ylabel('I am y')
new_ticks = np.linspace(-1,2,5)#5為5個單位
print(new_ticks)
plt.xticks(new_ticks)
#用r(正則表達)和$框起來可以轉換為計算機可以讀的字體
#加空格轉義為空格,加alpha能夠輸出 真正的alpha
plt.yticks([-2,-1.8,-1,1.22,3,],
[r'$really bad$',r'$bad alpha$','normal','good','really good'])
ax = plt.gca()#ax為上圖
ax.spines['right'].set_color('none')#刪除右邊緣黑框
ax.spines['top'].set_color('none')#刪除上邊緣黑框
ax.xaxis.set_ticks_position('bottom')#令x軸為底邊緣
ax.yaxis.set_ticks_position('left')#令y軸為左邊緣
ax.spines['bottom'].set_position(('data',-1))#將底邊緣放到 y軸數據-1的位置
ax.spines['left'].set_position(('data',0))#將左邊緣放到 y軸數據-1的位置
plt.show()
標注:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3,3,50)
y = 2*x+1
plt.figure(num=1,figsize=(8,5),)
plt.plot(x,y,)
ax = plt.gca()#ax為上圖
ax.spines['right'].set_color('none')#刪除右邊緣黑框
ax.spines['top'].set_color('none')#刪除上邊緣黑框
ax.xaxis.set_ticks_position('bottom')#令x軸為底邊緣
ax.yaxis.set_ticks_position('left')#令y軸為左邊緣
ax.spines['bottom'].set_position(('data',0))#將底邊緣放到 y軸數據-1的位置
ax.spines['left'].set_position(('data',0))#將左邊緣放到 y軸數據-1的位置
#添加標注
x0 =1
y0 = 2*x0+1
plt.scatter(x0,y0,s=50,color='b')#b代表blue
#plot(x列表,y列表)
plt.plot([x0,x0],[y0,y0],'k--',lw=2.5) #k代表黑色,lw為線寬
#model 1
#annotate標注
#xy為基準點
#textcoords='offset point'代表以這個點為基準,標注在這個點的基礎上x+30,y-30
#arrowprops描述箭頭,線的弧度等信息
#xycoords='data' xy的坐標是基于data的
plt.annotate(r'$2x+1=%s$' % y0,xy=(x0,y0),xycoords='data',xytext=(+30,-30),
textcoords='offset points',fontsize=16,
arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.2'))
#mothod 2
plt.text(-3.7,3,r'$This is the some text. mu sigma_i alpha_t$',
fontdict={'size':16, 'color':'r'})
plt.show()
(1)annotate語法說明:annotate(s='str' ,xy=(x,y) ,xytext=(l1,l2) ,..)
s 為注釋文本內容
xy 為被注釋的坐標點
xytext 為注釋文字的坐標位置
xycoords 參數如下:
figure points points from the lower left of the figure 點在圖左下方
figure pixels pixels from the lower left of the figure 圖左下角的像素
figure fraction fraction of figure from lower left 左下角數字部分
axes points points from lower left corner of axes 從左下角點的坐標
axes pixels pixels from lower left corner of axes 從左下角的像素坐標
axes fraction fraction of axes from lower left 左下角部分
data use the coordinate system of the object being annotated(default) 使用的坐標系統被注釋的對象(默認)
polar(theta,r) if not native ‘data’ coordinatest
extcoords設置注釋文字偏移量
| 參數 | 坐標系 |
|'figure points'| 距離圖形左下角的點數量 |
|'figure pixels'| 距離圖形左下角的像素數量 |
|'figure fraction'| 0,0 是圖形左下角,1,1 是右上角 |
|'axes points'| 距離軸域左下角的點數量 |
|'axes pixels'| 距離軸域左下角的像素數量 |
|'axes fraction'| 0,0 是軸域左下角,1,1 是右上角 |
|'data'| 使用軸域數據坐標系 |
arrowprops #箭頭參數,參數類型為字典dict
width the width of the arrow in points 點箭頭的寬度
headwidth the width of the base of the arrow head in points 在點的箭頭底座的寬度
headlength the length of the arrow head in points 點箭頭的長度
shrink fraction of total length to ‘shrink’ from both ends 總長度為分數“縮水”從兩端
facecolor 箭頭顏色
bbox給標題增加外框 ,常用參數如下:
boxstyle方框外形
facecolor(簡寫fc)背景顏色
edgecolor(簡寫ec)邊框線條顏色
edgewidth邊框線條大小
bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='k',lw=1 ,alpha=0.5) #fc為facecolor,ec為edgecolor,lw為lineweight
設置不透明度:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3,3,50)
y = 0.1*x
plt.figure()
#alpha為設置不透明深度
plt.plot(x,y,linewidth=10,alpha=0.7)
plt.ylim(-2,2)
#建立坐標系
ax = plt.gca()#ax為上圖
ax.spines['right'].set_color('none')#刪除右邊緣黑框
ax.spines['top'].set_color('none')#刪除上邊緣黑框
ax.xaxis.set_ticks_position('bottom')#令x軸為底邊緣
ax.yaxis.set_ticks_position('left')#令y軸為左邊緣
ax.spines['bottom'].set_position(('data',0))#將底邊緣放到 y軸數據-1的位置
ax.spines['left'].set_position(('data',0))#將左邊緣放到 y軸數據-1的位置
#解決線太粗把坐標擋住的問題
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(18)
label.set_bbox(dict(facecolor='yellow', edgecolor='None', alpha=0.7))
plt.show()
總結
以上是生活随笔為你收集整理的python 可视化 二维坐标标注等等的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 年份对应的中国生肖
- 下一篇: Flask-Limiter使用详细说明