欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

python matplotlib畫圖的幾個實例--latex,坐標系等

系統 2927 0

文章目錄

    • 實例1 學會使用tex/latex
    • 實例2 學會畫坐標軸
      • 2.1過程
      • 2.2 典型例子
        • 2.2.1 一條帶箭頭的豎線
        • 2.2.2 坐標系
        • 2.2.3 坐標系上畫三角函數

實例1 學會使用tex/latex

python matplotlib畫圖的幾個實例--latex,坐標系等_第1張圖片
第一眼看這個圖的時候覺得很震撼,代碼來自官網useTex demo,以及Latex
代碼在本例題末尾
首先需要注意的是,使用latex時可能會比不使用慢,因為需要調用到latex里面的一些程序,但latex語言美觀。
使用latex最簡單的方式就是加’$'符號(記住要加r),同時在開頭使用rc,設置usetex=True
(第一次調用會比較耗時間,之后就比較快了)

            
              import matplotlib.pyplot as plt
plt.rc('text', usetex=True)

            
          

如一個有latex公式的三角函數圖
python matplotlib畫圖的幾個實例--latex,坐標系等_第2張圖片

            
              
                import
              
               numpy 
              
                as
              
               np

              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               plt
plt
              
                .
              
              rc
              
                (
              
              
                'text'
              
              
                ,
              
               usetex
              
                =
              
              
                True
              
              
                )
              
              
                #使用latex
              
              

x 
              
                =
              
               np
              
                .
              
              linspace
              
                (
              
              
                -
              
              
                3
              
              
                ,
              
              
                3
              
              
                ,
              
              
                100
              
              
                )
              
              
y 
              
                =
              
               np
              
                .
              
              sin
              
                (
              
              x
              
                )
              
              

plt
              
                .
              
              plot
              
                (
              
              x
              
                ,
              
              y
              
                ,
              
              
                'r'
              
              
                )
              
              
plt
              
                .
              
              xlabel
              
                (
              
              r
              
                '$\theta$'
              
              
                )
              
              
                #一定要加r轉義,避免將$讀錯
              
              
plt
              
                .
              
              ylabel
              
                (
              
              r
              
                '$\delta$'
              
              
                )
              
              
plt
              
                .
              
              text
              
                (
              
              
                -
              
              
                1.5
              
              
                ,
              
              
                .5
              
              
                ,
              
              r
              
                '$\Omega=\theta+\delta+\phi$'
              
              
                )
              
              
                #在位置(-1.5,0.5)處開始寫公式
              
              
plt
              
                .
              
              show
              
                (
              
              
                )
              
            
          

下面的代碼是上面第一個圖的代碼

            
              
                import
              
               numpy 
              
                as
              
               np

              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               plt
plt
              
                .
              
              rc
              
                (
              
              
                'text'
              
              
                ,
              
               usetex
              
                =
              
              
                True
              
              
                )
              
              
                # interface tracking profiles
              
              
N 
              
                =
              
              
                500
              
              
delta 
              
                =
              
              
                0.6
              
              
X 
              
                =
              
               np
              
                .
              
              linspace
              
                (
              
              
                -
              
              
                1
              
              
                ,
              
              
                1
              
              
                ,
              
               N
              
                )
              
              
plt
              
                .
              
              plot
              
                (
              
              X
              
                ,
              
              
                (
              
              
                1
              
              
                -
              
               np
              
                .
              
              tanh
              
                (
              
              
                4
              
              
                *
              
               X 
              
                /
              
               delta
              
                )
              
              
                )
              
              
                /
              
              
                2
              
              
                ,
              
              
                # phase field tanh profiles
              
              
         X
              
                ,
              
              
                (
              
              
                1.4
              
              
                +
              
               np
              
                .
              
              tanh
              
                (
              
              
                4
              
              
                *
              
               X 
              
                /
              
               delta
              
                )
              
              
                )
              
              
                /
              
              
                4
              
              
                ,
              
              
                "C2"
              
              
                ,
              
              
                # composition profile
              
              
         X
              
                ,
              
               X 
              
                <
              
              
                0
              
              
                ,
              
              
                'k--'
              
              
                )
              
              
                # sharp interface
              
              
                # legend
              
              
plt
              
                .
              
              legend
              
                (
              
              
                (
              
              
                'phase field'
              
              
                ,
              
              
                'level set'
              
              
                ,
              
              
                'sharp interface'
              
              
                )
              
              
                ,
              
              
           shadow
              
                =
              
              
                True
              
              
                ,
              
               loc
              
                =
              
              
                (
              
              
                0.01
              
              
                ,
              
              
                0.48
              
              
                )
              
              
                ,
              
               handlelength
              
                =
              
              
                1.5
              
              
                ,
              
               fontsize
              
                =
              
              
                16
              
              
                )
              
              
                # the arrow
              
              
plt
              
                .
              
              annotate
              
                (
              
              
                ""
              
              
                ,
              
               xy
              
                =
              
              
                (
              
              
                -
              
              delta 
              
                /
              
              
                2
              
              
                .
              
              
                ,
              
              
                0.1
              
              
                )
              
              
                ,
              
               xytext
              
                =
              
              
                (
              
              delta 
              
                /
              
              
                2
              
              
                .
              
              
                ,
              
              
                0.1
              
              
                )
              
              
                ,
              
              
             arrowprops
              
                =
              
              
                dict
              
              
                (
              
              arrowstyle
              
                =
              
              
                "<->"
              
              
                ,
              
               connectionstyle
              
                =
              
              
                "arc3"
              
              
                )
              
              
                )
              
              
plt
              
                .
              
              text
              
                (
              
              
                0
              
              
                ,
              
              
                0.1
              
              
                ,
              
               r
              
                '$\delta$'
              
              
                ,
              
              
                {
              
              
                'color'
              
              
                :
              
              
                'black'
              
              
                ,
              
              
                'fontsize'
              
              
                :
              
              
                24
              
              
                ,
              
              
                'ha'
              
              
                :
              
              
                'center'
              
              
                ,
              
              
                'va'
              
              
                :
              
              
                'center'
              
              
                ,
              
              
                'bbox'
              
              
                :
              
              
                dict
              
              
                (
              
              boxstyle
              
                =
              
              
                "round"
              
              
                ,
              
               fc
              
                =
              
              
                "white"
              
              
                ,
              
               ec
              
                =
              
              
                "black"
              
              
                ,
              
               pad
              
                =
              
              
                0.2
              
              
                )
              
              
                }
              
              
                )
              
              
                # Use tex in labels
              
              
plt
              
                .
              
              xticks
              
                (
              
              
                (
              
              
                -
              
              
                1
              
              
                ,
              
              
                0
              
              
                ,
              
              
                1
              
              
                )
              
              
                ,
              
              
                (
              
              
                '$-1$'
              
              
                ,
              
               r
              
                '$\pm 0$'
              
              
                ,
              
              
                '$+1$'
              
              
                )
              
              
                ,
              
               color
              
                =
              
              
                'k'
              
              
                ,
              
               size
              
                =
              
              
                20
              
              
                )
              
              
                # Left Y-axis labels, combine math mode and text mode
              
              
plt
              
                .
              
              ylabel
              
                (
              
              r
              
                '\bf{phase field} $\phi$'
              
              
                ,
              
              
                {
              
              
                'color'
              
              
                :
              
              
                'C0'
              
              
                ,
              
              
                'fontsize'
              
              
                :
              
              
                20
              
              
                }
              
              
                )
              
              
plt
              
                .
              
              yticks
              
                (
              
              
                (
              
              
                0
              
              
                ,
              
              
                0.5
              
              
                ,
              
              
                1
              
              
                )
              
              
                ,
              
              
                (
              
              r
              
                '\bf{0}'
              
              
                ,
              
               r
              
                '\bf{.5}'
              
              
                ,
              
               r
              
                '\bf{1}'
              
              
                )
              
              
                ,
              
               color
              
                =
              
              
                'k'
              
              
                ,
              
               size
              
                =
              
              
                20
              
              
                )
              
              
                # Right Y-axis labels
              
              
plt
              
                .
              
              text
              
                (
              
              
                1.02
              
              
                ,
              
              
                0.5
              
              
                ,
              
               r
              
                "\bf{level set} $\phi$"
              
              
                ,
              
              
                {
              
              
                'color'
              
              
                :
              
              
                'C2'
              
              
                ,
              
              
                'fontsize'
              
              
                :
              
              
                20
              
              
                }
              
              
                ,
              
              
         horizontalalignment
              
                =
              
              
                'left'
              
              
                ,
              
              
         verticalalignment
              
                =
              
              
                'center'
              
              
                ,
              
              
         rotation
              
                =
              
              
                90
              
              
                ,
              
              
         clip_on
              
                =
              
              
                False
              
              
                ,
              
              
         transform
              
                =
              
              plt
              
                .
              
              gca
              
                (
              
              
                )
              
              
                .
              
              transAxes
              
                )
              
              
                # Use multiline environment inside a `text`.
              
              
                # level set equations
              
              
eq1 
              
                =
              
               r
              
                "\begin{eqnarray*}"
              
              
                +
              
               \
      r
              
                "|\nabla\phi| &=& 1,\\"
              
              
                +
              
               \
      r
              
                "\frac{\partial \phi}{\partial t} + U|\nabla \phi| &=& 0 "
              
              
                +
              
               \
      r
              
                "\end{eqnarray*}"
              
              
plt
              
                .
              
              text
              
                (
              
              
                1
              
              
                ,
              
              
                0.9
              
              
                ,
              
               eq1
              
                ,
              
              
                {
              
              
                'color'
              
              
                :
              
              
                'C2'
              
              
                ,
              
              
                'fontsize'
              
              
                :
              
              
                18
              
              
                }
              
              
                ,
              
               va
              
                =
              
              
                "top"
              
              
                ,
              
               ha
              
                =
              
              
                "right"
              
              
                )
              
              
                # phase field equations
              
              
eq2 
              
                =
              
               r
              
                '\begin{eqnarray*}'
              
              
                +
              
               \
      r
              
                '\mathcal{F} &=& \int f\left( \phi, c \right) dV, \\ '
              
              
                +
              
               \
      r
              
                '\frac{ \partial \phi } { \partial t } &=& -M_{ \phi } '
              
              
                +
              
               \
      r
              
                '\frac{ \delta \mathcal{F} } { \delta \phi }'
              
              
                +
              
               \
      r
              
                '\end{eqnarray*}'
              
              
plt
              
                .
              
              text
              
                (
              
              
                0.18
              
              
                ,
              
              
                0.18
              
              
                ,
              
               eq2
              
                ,
              
              
                {
              
              
                'color'
              
              
                :
              
              
                'C0'
              
              
                ,
              
              
                'fontsize'
              
              
                :
              
              
                16
              
              
                }
              
              
                )
              
              

plt
              
                .
              
              text
              
                (
              
              
                -
              
              
                1
              
              
                ,
              
              
                .30
              
              
                ,
              
               r
              
                'gamma: $\gamma$'
              
              
                ,
              
              
                {
              
              
                'color'
              
              
                :
              
              
                'r'
              
              
                ,
              
              
                'fontsize'
              
              
                :
              
              
                20
              
              
                }
              
              
                )
              
              
plt
              
                .
              
              text
              
                (
              
              
                -
              
              
                1
              
              
                ,
              
              
                .18
              
              
                ,
              
               r
              
                'Omega: $\Omega$'
              
              
                ,
              
              
                {
              
              
                'color'
              
              
                :
              
              
                'b'
              
              
                ,
              
              
                'fontsize'
              
              
                :
              
              
                20
              
              
                }
              
              
                )
              
            
          

實例2 學會畫坐標軸

一個簡單的方法是直接畫兩條線,代表x,y,但是這樣不美觀,也不好用

2.1過程

matplotlib畫圖的時候總是默認的方框,有時候想展示的是x,y左邊軸。
繪制坐標系需要用到axisartist,這個可以參考官網axisartist
例外也可以參考博文Python-matplotlib繪制帶箭頭x-y坐標軸圖形
官網上給的一個復雜的demo是
python matplotlib畫圖的幾個實例--latex,坐標系等_第3張圖片
可見axisartist在畫線上還是很實用的。
所以本小節從axisartist說起
創建子圖
子圖是通過subplot來繪制的,111表示一行一列只有一個圖,第三個1是表示第一個圖。
如221表示有兩行兩列(則有四個圖)第一個圖,分別展示如下。

            
              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               plt

              
                import
              
               mpl_toolkits
              
                .
              
              axisartist 
              
                as
              
               AA

fig 
              
                =
              
               plt
              
                .
              
              figure
              
                (
              
              
                )
              
              
                #創建畫布
              
              
ax 
              
                =
              
               AA
              
                .
              
              Subplot
              
                (
              
              fig
              
                ,
              
              
                111
              
              
                )
              
              
fig
              
                .
              
              add_axes
              
                (
              
              ax
              
                )
              
              
                #將坐標ax加入畫布中
              
            
          

(111)
python matplotlib畫圖的幾個實例--latex,坐標系等_第4張圖片
(221)
python matplotlib畫圖的幾個實例--latex,坐標系等_第5張圖片
上下左右(top, bottom,left,right)共四條線,即四個坐標軸,下面隱藏右邊和上邊

            
              ax
              
                .
              
              axis
              
                [
              
              
                'right'
              
              
                ]
              
              
                .
              
              set_visible
              
                (
              
              
                False
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'top'
              
              
                ]
              
              
                .
              
              set_visible
              
                (
              
              
                False
              
              
                )
              
            
          

或者

            
              ax.axis['right','top'].set_visible(False)

            
          

python matplotlib畫圖的幾個實例--latex,坐標系等_第6張圖片
在y=0處加橫坐標(horizontal axis)

            
              
                #在第一個軸(即x軸,nth_coord是nth coordinate的簡稱,即第n個坐標軸)
              
              
                #當nth_coord=1表示x軸。
              
              
                #所以nth_coord=0, value=0表示畫一條x軸,并經過y=0點。
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y=0'
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              nth_coord
              
                =
              
              
                0
              
              
                ,
              
               value
              
                =
              
              
                0
              
              
                )
              
              
                # 可以將參數名去掉直接寫(0,0)
              
            
          
            
              
                #畫一條經過x=0.5的y軸,圖見下
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x=0.5'
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              nth_coord
              
                =
              
              
                1
              
              
                ,
              
               value
              
                =
              
              
                0.5
              
              
                )
              
            
          

結合上面的知識,我們可以畫出如下的圖形
python matplotlib畫圖的幾個實例--latex,坐標系等_第7張圖片
代碼為

            
              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               plt

              
                import
              
               mpl_toolkits
              
                .
              
              axisartist 
              
                as
              
               AA

fig 
              
                =
              
               plt
              
                .
              
              figure
              
                (
              
              
                )
              
              
                #創建畫布
              
              
ax 
              
                =
              
               AA
              
                .
              
              Subplot
              
                (
              
              fig
              
                ,
              
              
                111
              
              
                )
              
              
fig
              
                .
              
              add_axes
              
                (
              
              ax
              
                )
              
              
                #將坐標ax加入畫布中
              
              
                #設置某些坐標軸不可見
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'right'
              
              
                ,
              
              
                'top'
              
              
                ,
              
              
                'bottom'
              
              
                ]
              
              
                .
              
              set_visible
              
                (
              
              
                False
              
              
                )
              
              
                # 增加浮動軸
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y=0'
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              nth_coord
              
                =
              
              
                0
              
              
                ,
              
               value
              
                =
              
              
                0
              
              
                )
              
              
                #如果不想顯示坐標軸上的數字可以如下
              
              
                #ax.axis["left"].toggle(ticklabels=False)
              
              
                #給坐標軸添加文本
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y=0'
              
              
                ]
              
              
                .
              
              label
              
                .
              
              set_text
              
                (
              
              
                'y=0'
              
              
                )
              
              
                #將數字變成紅色
              
              
ax
              
                .
              
              axis
              
                [
              
              
                "left"
              
              
                ]
              
              
                .
              
              major_ticklabels
              
                .
              
              set_color
              
                (
              
              
                "r"
              
              
                )
              
              
                #所有軸的數字都變成紅色
              
              
                #ax.axis[:].major_ticklabels.set_color("r")
              
              
                #y軸范圍
              
              
ax
              
                .
              
              set_ylim
              
                (
              
              
                -
              
              
                4
              
              
                ,
              
              
                4
              
              
                )
              
              

plt
              
                .
              
              show
              
                (
              
              
                )
              
            
          

如果是畫一條經過x=0.5的y軸
python matplotlib畫圖的幾個實例--latex,坐標系等_第8張圖片
畫箭頭

            
              ax
              
                .
              
              axis
              
                [
              
              
                'y=0'
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                '->'
              
              
                ,
              
              size
              
                =
              
              
                1.5
              
              
                )
              
            
          

2.2 典型例子

2.2.1 一條帶箭頭的豎線

python matplotlib畫圖的幾個實例--latex,坐標系等_第9張圖片
python matplotlib畫圖的幾個實例--latex,坐標系等_第10張圖片
(圖一:數字顯示在左邊,實心箭頭)
python matplotlib畫圖的幾個實例--latex,坐標系等_第11張圖片
(圖2:數字顯示在右邊)

            
              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               plt

              
                import
              
               mpl_toolkits
              
                .
              
              axisartist 
              
                as
              
               axisartist

              
                #定義一個畫軸線的函數
              
              
                def
              
              
                setup_axes
              
              
                (
              
              fig
              
                ,
              
               rect
              
                )
              
              
                :
              
              
    ax 
              
                =
              
               axisartist
              
                .
              
              Subplot
              
                (
              
              fig
              
                ,
              
               rect
              
                )
              
              
                #建立子圖
              
              
    fig
              
                .
              
              add_axes
              
                (
              
              ax
              
                )
              
              
                #子圖加入畫布
              
              

    ax
              
                .
              
              set_ylim
              
                (
              
              
                -
              
              
                0.1
              
              
                ,
              
              
                1.5
              
              
                )
              
              
    ax
              
                .
              
              set_yticks
              
                (
              
              
                [
              
              
                0
              
              
                ,
              
              
                1
              
              
                ]
              
              
                )
              
              

    ax
              
                .
              
              axis
              
                [
              
              
                :
              
              
                ]
              
              
                .
              
              set_visible
              
                (
              
              
                False
              
              
                )
              
              
                #四條邊框線都消失
              
              

    ax
              
                .
              
              axis
              
                [
              
              
                "x"
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              
                1
              
              
                ,
              
              
                0.5
              
              
                )
              
              
                #y軸經過x=0.5
              
              
    ax
              
                .
              
              axis
              
                [
              
              
                "x"
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                "->"
              
              
                ,
              
               size
              
                =
              
              
                1.5
              
              
                )
              
              
                #空心箭頭
              
              
                #('-|>')實心箭頭
              
              
                return
              
               ax


fig 
              
                =
              
               plt
              
                .
              
              figure
              
                (
              
              figsize
              
                =
              
              
                (
              
              
                3
              
              
                ,
              
              
                2.5
              
              
                )
              
              
                )
              
              
                #建立畫布,尺寸為長3寬2.5
              
              
fig
              
                .
              
              subplots_adjust
              
                (
              
              top
              
                =
              
              
                0.8
              
              
                )
              
              
                #可不要
              
              
ax1 
              
                =
              
               setup_axes
              
                (
              
              fig
              
                ,
              
              
                "111"
              
              
                )
              
              
                #“111”可以寫成111,即str或者int都可以
              
              

ax1
              
                .
              
              axis
              
                [
              
              
                "x"
              
              
                ]
              
              
                .
              
              set_axis_direction
              
                (
              
              
                "left"
              
              
                )
              
              
                #數字顯示在左邊right則右邊
              
              

plt
              
                .
              
              show
              
                (
              
              
                )
              
            
          

2.2.2 坐標系

python matplotlib畫圖的幾個實例--latex,坐標系等_第12張圖片

            
              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               plt

              
                import
              
               mpl_toolkits
              
                .
              
              axisartist 
              
                as
              
               axisartist


              
                def
              
              
                setup_axes
              
              
                (
              
              fig
              
                ,
              
               rect
              
                )
              
              
                :
              
              
    ax 
              
                =
              
               axisartist
              
                .
              
              Subplot
              
                (
              
              fig
              
                ,
              
               rect
              
                )
              
              
    fig
              
                .
              
              add_axes
              
                (
              
              ax
              
                )
              
              
                #手動設置y軸,從-0.1開始,我對x軸不做設置
              
              
                #大家可以對比x跟y有什么不同
              
              
    ax
              
                .
              
              set_ylim
              
                (
              
              
                -
              
              
                0.1
              
              
                ,
              
              
                1.5
              
              
                )
              
              
    ax
              
                .
              
              set_yticks
              
                (
              
              
                [
              
              
                0
              
              
                ,
              
              
                0.1
              
              
                ,
              
              
                0.5
              
              
                ]
              
              
                )
              
              
                #手動寫參數
              
              
                #4個邊框線不可見
              
              
    ax
              
                .
              
              axis
              
                [
              
              
                :
              
              
                ]
              
              
                .
              
              set_visible
              
                (
              
              
                False
              
              
                )
              
              
                #第2條線,即y軸
              
              
    ax
              
                .
              
              axis
              
                [
              
              
                "y"
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              
                1
              
              
                ,
              
              
                0
              
              
                )
              
              
    ax
              
                .
              
              axis
              
                [
              
              
                "y"
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                "-|>"
              
              
                ,
              
               size
              
                =
              
              
                1.5
              
              
                )
              
              
                #第一條線,x軸
              
              
    ax
              
                .
              
              axis
              
                [
              
              
                "x"
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              
                0
              
              
                ,
              
              
                0
              
              
                )
              
              
    ax
              
                .
              
              axis
              
                [
              
              
                "x"
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                "-|>"
              
              
                ,
              
               size
              
                =
              
              
                1.5
              
              
                )
              
              
                return
              
              
                (
              
              ax
              
                )
              
              

fig 
              
                =
              
               plt
              
                .
              
              figure
              
                (
              
              figsize
              
                =
              
              
                (
              
              
                8
              
              
                ,
              
              
                8
              
              
                )
              
              
                )
              
              
                #設置畫布大小(建議可以默認為空括號,不設置大小)
              
              
ax1 
              
                =
              
               setup_axes
              
                (
              
              fig
              
                ,
              
              
                111
              
              
                )
              
              
ax1
              
                .
              
              axis
              
                [
              
              
                "x"
              
              
                ]
              
              
                .
              
              set_axis_direction
              
                (
              
              
                "bottom"
              
              
                )
              
              
                #x軸數字顯示在線的下方
              
              
ax1
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                .
              
              set_axis_direction
              
                (
              
              
                'right'
              
              
                )
              
              
                #y軸數字顯示在線的右邊
              
              

plt
              
                .
              
              show
              
                (
              
              
                )
              
            
          

2.2.3 坐標系上畫三角函數

python matplotlib畫圖的幾個實例--latex,坐標系等_第13張圖片

            
              
                import
              
               mpl_toolkits
              
                .
              
              axisartist 
              
                as
              
               axisartist

              
                import
              
               numpy 
              
                as
              
               np


              
                #定義坐標軸函數
              
              
                def
              
              
                setup_axes
              
              
                (
              
              fig
              
                ,
              
               rect
              
                )
              
              
                :
              
              
    ax 
              
                =
              
               axisartist
              
                .
              
              Subplot
              
                (
              
              fig
              
                ,
              
               rect
              
                )
              
              
    fig
              
                .
              
              add_axes
              
                (
              
              ax
              
                )
              
              

    ax
              
                .
              
              set_ylim
              
                (
              
              
                -
              
              
                10
              
              
                ,
              
              
                10
              
              
                )
              
              
                #自定義刻度
              
              
                #    ax.set_yticks([-10, 0,9])
              
              
    ax
              
                .
              
              set_xlim
              
                (
              
              
                -
              
              
                10
              
              
                ,
              
              
                10
              
              
                )
              
              
    ax
              
                .
              
              axis
              
                [
              
              
                :
              
              
                ]
              
              
                .
              
              set_visible
              
                (
              
              
                False
              
              
                )
              
              
                #第2條線,即y軸,經過x=0的點
              
              
    ax
              
                .
              
              axis
              
                [
              
              
                "y"
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              
                1
              
              
                ,
              
              
                0
              
              
                )
              
              
    ax
              
                .
              
              axis
              
                [
              
              
                "y"
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                "-|>"
              
              
                ,
              
               size
              
                =
              
              
                1.5
              
              
                )
              
              
                #    第一條線,x軸,經過y=0的點
              
              
    ax
              
                .
              
              axis
              
                [
              
              
                "x"
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              
                0
              
              
                ,
              
              
                0
              
              
                )
              
              
    ax
              
                .
              
              axis
              
                [
              
              
                "x"
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                "-|>"
              
              
                ,
              
               size
              
                =
              
              
                1.5
              
              
                )
              
              
                return
              
              
                (
              
              ax
              
                )
              
              
                #設置畫布
              
              
fig 
              
                =
              
               plt
              
                .
              
              figure
              
                (
              
              figsize
              
                =
              
              
                (
              
              
                8
              
              
                ,
              
              
                8
              
              
                )
              
              
                )
              
              
                #建議可以直接plt.figure()不定義大小
              
              
ax1 
              
                =
              
               setup_axes
              
                (
              
              fig
              
                ,
              
              
                111
              
              
                )
              
              
ax1
              
                .
              
              axis
              
                [
              
              
                "x"
              
              
                ]
              
              
                .
              
              set_axis_direction
              
                (
              
              
                "bottom"
              
              
                )
              
              
ax1
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                .
              
              set_axis_direction
              
                (
              
              
                'right'
              
              
                )
              
              
                #在已經定義好的畫布上加入三角函數
              
              
x 
              
                =
              
               np
              
                .
              
              arange
              
                (
              
              
                -
              
              
                15
              
              
                ,
              
              
                15
              
              
                ,
              
              
                0.1
              
              
                )
              
              
y 
              
                =
              
               np
              
                .
              
              sin
              
                (
              
              x
              
                )
              
              
plt
              
                .
              
              plot
              
                (
              
              x
              
                ,
              
              y
              
                )
              
              

plt
              
                .
              
              show
              
                (
              
              
                )
              
            
          

更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日韩中文一区二区三区 | 日本精品欧洲www | 欧美性视频网 | 亚洲情a成黄在线观看动 | 国产精品久久久久久无码人妻 | 成人免费视频在线观看 | 精品一区视频 | 欧美日韩综合精品 | 欧美va在线观看 | 久操久热 | 91亚洲影院| 国产精品成人亚洲一区二区 | 国产精品三级a三级三级午夜 | 亚洲一区二区免费看 | 激情 婷婷 | 国产大尺度吃奶无遮无挡网 | 久久中文字幕美谷朱里 | 精品免费视频 | 中文字幕日韩在线 | 奇米影视亚洲精品一区 | 色婷婷亚洲五月色综合色 | 亚洲AV久久无码精品九九九小说 | 日本色播| 国产福利不卡视频在免费播放 | 色欲AV色情国产又爽又色 | 国产一区二区黑人欧美xxxx | 米奇精品一区二区三区在线观看 | 成在线人视频免费视频 | 亚洲v日本v欧美v综合v | 久久久久久久久女黄 | 免费av在线播放 | 欧美成熟丰满老妇xxxx | 亚洲天堂网2018 | 日韩在线观看视频网站 | 男女午夜性爽快免费视频不卡 | 狠狠操伊人 | 一级毛片免费视频 | 男女爽爽无遮挡午夜动态图 | 亚洲欧美不卡 | 中文字幕日韩欧美 | 欧美一区二区三区在线观看免费 |