小试牛刀Matplotlib
1、plt.subplot()
import matplotlib.pyplot as plt# Integer subplot specification must be a three digit number # 前兩位代表橫豎長度比 # 左邊的代表橫,中間的代標縱坐標,右邊的則表示繪圖位置(當橫縱比不是1:1時) ax1, ax2, ax3 = plt.subplot(231), plt.subplot(232), plt.subplot(233) ax4, ax5, ax6 = plt.subplot(234), plt.subplot(235), plt.subplot(236) # 被注釋的地方xy(x, y)和插入文本的地方xytext(x, y) ax2.annotate("Test", xy=(0.5, 0.5), xycoords=ax1.transData,xytext=(0.5, 0.5), textcoords=ax2.transData,arrowprops=dict(arrowstyle="<-")) ax3.annotate("ax3", xy=(0.5, 0.5), xytext=(0.5, 0.5)) ax4.annotate("ax4", xy=(0.5, 0.5), xytext=(0.5, 0.5)) ax5.annotate("ax5", xy=(0.5, 0.5), xytext=(0.5, 0.5)) ax6.annotate("ax6", xy=(0.5, 0.5), xytext=(0.5, 0.5)) plt.show()2、annotate
- 被注釋的地方xy(x, y)和插入文本的地方xytext(x, y)
- xycoords和textcoords指定xy和xytext的坐標系。默認為data(使用軸域數(shù)據(jù)坐標系)
| 參數(shù) | 坐標系 |?
|? 'figure points' ?| 距離圖形左下角的點數(shù)量 |?|? 'figure pixels' ?| 距離圖形左下角的像素數(shù)量 |?
|? 'figure fraction' ?| 0,0 是圖形左下角,1,1 是右上角 |?
|? 'axes points' ?| 距離軸域左下角的點數(shù)量 |?
|? 'axes pixels' ?| 距離軸域左下角的像素數(shù)量 |?
|? 'axes fraction' ?| 0,0 是軸域左下角,1,1 是右上角 |?
|?'data'?| 使用軸域數(shù)據(jù)坐標系 |
import matplotlib.pyplot as plt from numpy import *ax1 = plt.subplot(111)t = arange(0.0, 5.0, 0.01) s = cos(2*pi*t) line = plt.plot(t, s, lw=2) # 被注釋的地方xy(x, y)和插入文本的地方xytext(x, y) # xycoords和textcoords指定xy和xytext的坐標系。默認為data(使用軸域數(shù)據(jù)坐標系)ax1.annotate('local max', xy=(2, 1), xytext=(3, 1.5),arrowprops=dict(facecolor='black'))plt.ylim(-2, 2) plt.show()3、在機器學習實戰(zhàn)的第三章,我們還用到了箭頭(arrow)和文本框(box)
import matplotlib.pyplot as plt# boxstyle文本框樣式, fc(face color)背景透明度 decisionNode = dict(boxstyle="round4, pad=0.5", fc="0.8") leafNode = dict(boxstyle="circle", fc="0.8") # 箭頭樣式 arrow_args = dict(arrowstyle="<-")def plotNode(nodeTxt, centerPt, parentPt, nodeType):createPlot.ax1.annotate(nodeTxt, xy=parentPt, xycoords="axes fraction",xytext=centerPt, textcoords="axes fraction", va="center",ha="center", bbox=nodeType, arrowprops=arrow_args)def createPlot():fig = plt.figure(1, facecolor="white")fig.clf()createPlot.ax1 = plt.subplot(111, frameon=False)plotNode('決策點', (0.5, 0.1), (0.1, 0.5), decisionNode)plotNode('葉節(jié)點', (0.8, 0.1), (0.3, 0.8), leafNode)# plt.rcParams['font.sans-serif'] = ['SimHei']plt.show()createPlot()- Annotating with Text with Box:boxstyle
Class | Name | Attrs |
Circle | circle | pad=0.3 |
DArrow | darrow | pad=0.3 |
LArrow | larrow | pad=0.3 |
RArrow | rarrow | pad=0.3 |
Round | round | pad=0.3,rounding_size=None |
Round4 | round4 | pad=0.3,rounding_size=None |
Roundtooth | roundtooth | pad=0.3,tooth_size=None |
Sawtooth | sawtooth | pad=0.3,tooth_size=None |
Square | square | pad=0.3 |
- Annotating with Arrow:arrowstyle
Name | Attrs |
- | None |
-> | head_length=0.4,head_width=0.2 |
-[ | widthB=1.0,lengthB=0.2,angleB=None |
|-| | widthA=1.0,widthB=1.0 |
-|> | head_length=0.4,head_width=0.2 |
<- | head_length=0.4,head_width=0.2 |
<-> | head_length=0.4,head_width=0.2 |
<|- | head_length=0.4,head_width=0.2 |
<|-|> | head_length=0.4,head_width=0.2 |
fancy | head_length=0.4,head_width=0.4,tail_width=0.4 |
simple | head_length=0.5,head_width=0.5,tail_width=0.2 |
wedge | tail_width=0.3,shrink_factor=0.5 |
總結(jié)
以上是生活随笔為你收集整理的小试牛刀Matplotlib的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Docker - 安装并持久化Postg
- 下一篇: 《机器学习实战》chapter03 决策