Pin-Jiun / Python

Python Document
0 stars 0 forks source link

25-matplotlib(1) #25

Open Pin-Jiun opened 1 year ago

Pin-Jiun commented 1 year ago

matplotlib.pyplot基本用法

import matplotlib.pyplot as plt
import numpy as np

x = np.linespace(-1,1,50)
y = 2*x + 1

plt.plot(x,y)
plt.show()

image

plt.figure()以下的表示一個fig的呈現

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-1,1,50)
y1 = 2*x + 1
y2 = x**2

plt.figure() #不給參數會默認從1開始排
plt.plot(x,y1)
#plt.show() 分別呈現, 關掉此才會show下一張

plt.figure(num=6, figsize=(8,5))
plt.plot(x,y2)
plt.plot(x,y1, color="red", linewidth = 1.0, linestyle="--")

plt.show() #會將上面還沒show的figure show()出來

Axis的設定

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-10,10,500)
y1 = 2*x + 1
y2 = x**2

plt.figure() #不給參數會默認從1開始排

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")

#用tick表示坐標軸的區間
new_ticks = np.linspace(-1,2,5)
print(new_ticks)
plt.xticks(new_ticks)

#使用兩個list使座標tick對應相關的文字
plt.yticks([-2,-1.8,-1,1.22,3],
            ["really bad","bad","normal","good","really good"])

plt.show() #會將上面還沒show的figure show()出來

將文字進行轉換

使用r"$really\ bad$"來表示正規表達式轉換字體

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-10,10,500)
y1 = 2*x + 1
y2 = x**2

plt.figure() #不給參數會默認從1開始排

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")

#用tick表示坐標軸的區間
new_ticks = np.linspace(-1,2,5)
print(new_ticks)
plt.xticks(new_ticks)

#使用兩個list使座標tick對應相關的文字
plt.yticks([-2,-1.8,-1,1.22,3],
            [r"$really\ bad$",r"$bad \alpha$", r"$normal$",r"$good$",r"$really good$"])

plt.show() #會將上面還沒show的figure show()出來

移動坐標軸的位置gca = get current axis

Pin-Jiun commented 1 year ago

圖例plt.legend()

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-10,10,500)
y1 = 2*x + 1
y2 = x**2

plt.figure() #不給參數會默認從1開始排

plt.xlim((-1,2))
plt.ylim((-2,3))

plt.xlabel("I am x")
plt.ylabel("I am y")

#用tick表示坐標軸的區間
new_ticks = np.linspace(-1,2,5)
print(new_ticks)
plt.xticks(new_ticks)

#使用兩個list使座標tick對應相關的文字
plt.yticks([-2,-1.8,-1,1.22,3],
            [r"$really\ bad$",r"$bad \alpha$", r"$normal$",r"$good$",r"$really good$"])

plt.plot(x,y2, label = "line1")
plt.plot(x,y1, color="red", linewidth = 1.0, linestyle="--", label = "line2")
plt.legend()使用默認設置        

plt.show() #會將上面還沒show的figure show()出來

如果要設置返回值, 則一定要將此物件後面設定一個, 此為matplot特殊設定的

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-10,10,500)
y1 = 2*x + 1
y2 = x**2

plt.figure() #不給參數會默認從1開始排

plt.xlim((-1,2))
plt.ylim((-2,3))

plt.xlabel("I am x")
plt.ylabel("I am y")

#用tick表示坐標軸的區間
new_ticks = np.linspace(-1,2,5)
print(new_ticks)
plt.xticks(new_ticks)

#使用兩個list使座標tick對應相關的文字
plt.yticks([-2,-1.8,-1,1.22,3],
            [r"$really\ bad$",r"$bad \alpha$", r"$normal$",r"$good$",r"$really good$"])

#如果要設置返回值, 則一定要將此物件後面設定一個,
l1, =plt.plot(x,y2, label = "line1")
l2, =plt.plot(x,y1, color="red", linewidth = 1.0, linestyle="--", label = "line2")
plt.legend(handles=[l1, l2], labels=["aaa","bbb"] , loc="best")            

plt.show() #會將上面還沒show的figure show()出來

image

Pin-Jiun commented 1 year ago

Annotation 標示, 註解

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.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))

x0 = 1
y0 = 2*x0 + 1

#plot給定兩個點可以畫出一條線
plt.plot([x0, x0,], [0, y0,], 'k--', linewidth=2.5)

#scatter表示的是點
plt.scatter([x0, ], [y0, ], s=50, color='b')

# method 1:
#####################
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"))

# method 2:
########################
plt.text(-3.7, 3, r'$This\ is\ the\ some\ text. \mu\ \sigma_i\ \alpha_t$',
         fontdict={'size': 16, 'color': 'r'})

plt.show()

image

Pin-Jiun commented 1 year ago

Tick的進階調整


import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-3, 3, 50)
y = 0.1*x

plt.figure()
plt.plot(x, y, linewidth=10, zorder=1)      # set zorder for ordering the plot in plt 2.0.2 or higher
plt.ylim(-2, 2)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))

for label in ax.get_xticklabels() + ax.get_yticklabels():
    label.set_fontsize(12)
    # set zorder for ordering the plot in plt 2.0.2 or higher
    #bbox表示後面的框框大小
    #alpha表示透明度, 0.8表示80%%可以穿透
    label.set_bbox(dict(facecolor='white', edgecolor='none', alpha=0.8, zorder=2))
plt.show()
Pin-Jiun commented 1 year ago

scatter圖

import matplotlib.pyplot as plt
import numpy as np

n = 1024    # data size
X = np.random.normal(0, 1, n)
Y = np.random.normal(0, 1, n)
T = np.arctan2(Y, X)    # for color 可以不用太注重此

plt.scatter(X, Y, s=75, c=T, alpha=.5)

plt.xlim(-1.5, 1.5)
plt.xticks(())  # ignore xticks
plt.ylim(-1.5, 1.5)
plt.yticks(())  # ignore yticks

plt.show()

image

Pin-Jiun commented 1 year ago

bar柱狀圖

import matplotlib.pyplot as plt
import numpy as np

n = 12
X = np.arange(n)
Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)

plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')

for x, y in zip(X, Y1):
    # ha: horizontal alignment
    # va: vertical alignment
    plt.text(x + 0.4, y + 0.05, '%.2f' % y, ha='center', va='bottom')

for x, y in zip(X, Y2):
    # ha: horizontal alignment
    # va: vertical alignment
    plt.text(x + 0.4, -y - 0.05, '%.2f' % y, ha='center', va='top')

plt.xlim(-.5, n)
plt.xticks(())
plt.ylim(-1.25, 1.25)
plt.yticks(())

plt.show()

image

Pin-Jiun commented 1 year ago

等高線圖

import matplotlib.pyplot as plt
import numpy as np

def f(x,y):
    # the height function
    return (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 -y**2)

n = 256
x = np.linspace(-3, 3, n)
y = np.linspace(-3, 3, n)
X,Y = np.meshgrid(x, y)

# use plt.contourf to filling contours
# X, Y and value for (X,Y) point
plt.contourf(X, Y, f(X, Y), 8, alpha=.75, cmap=plt.cm.hot)

# use plt.contour to add contour lines
C = plt.contour(X, Y, f(X, Y), 8, colors='black', linewidth=.5)
# adding label
plt.clabel(C, inline=True, fontsize=10)

plt.xticks(())
plt.yticks(())
plt.show()

image

Pin-Jiun commented 1 year ago

image圖片

import matplotlib.pyplot as plt
import numpy as np

# image data
a = np.array([0.313660827978, 0.365348418405, 0.423733120134,
              0.365348418405, 0.439599930621, 0.525083754405,
              0.423733120134, 0.525083754405, 0.651536351379]).reshape(3,3)

"""
for the value of "interpolation", check this:
http://matplotlib.org/examples/images_contours_and_fields/interpolation_methods.html
for the value of "origin"= ['upper', 'lower'], check this:
http://matplotlib.org/examples/pylab_examples/image_origin.html
"""

plt.imshow(a, interpolation='nearest', cmap='bone', origin='lower')
plt.colorbar()

plt.xticks(())
plt.yticks(())
plt.show()

#origin='upper呈現方式會和上面數字相同, 從上面開始放
plt.imshow(a, interpolation='hermite', cmap='bone', origin='upper')
plt.colorbar(shrink=.92)

plt.xticks(())
plt.yticks(())
plt.show()

image

Pin-Jiun commented 1 year ago

3D數據的顯示

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()

#ax = Axes3D(fig) 如果無法顯示更改成以下代碼
ax = fig.add_axes(Axes3D(fig))

# X, Y value
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X ** 2 + Y ** 2)
# height value
Z = np.sin(R)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
"""
============= ================================================
        Argument      Description
        ============= ================================================
        *X*, *Y*, *Z* Data values as 2D arrays
        *rstride*     Array row stride (step size), defaults to 10
        *cstride*     Array column stride (step size), defaults to 10
        *color*       Color of the surface patches
        *cmap*        A colormap for the surface patches.
        *facecolors*  Face colors for the individual patches
        *norm*        An instance of Normalize to map values to colors
        *vmin*        Minimum value to map
        *vmax*        Maximum value to map
        *shade*       Whether to shade the facecolors
        ============= ================================================
"""

# I think this is different from plt12_contours
ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.get_cmap('rainbow'))
"""
==========  ================================================
        Argument    Description
        ==========  ================================================
        *X*, *Y*,   Data values as numpy.arrays
        *Z*
        *zdir*      The direction to use: x, y or z (default)
        *offset*    If specified plot a projection of the filled contour
                    on this position in plane normal to zdir
        ==========  ================================================
"""

ax.set_zlim(-2, 2)

plt.show()

image

Pin-Jiun commented 1 year ago

subplot分隔顯示

import matplotlib.pyplot as plt

# example 1:
###############################
plt.figure(figsize=(6, 4))
# plt.subplot(n_rows, n_cols, plot_num)
plt.subplot(2, 2, 1)
plt.plot([0, 1], [0, 1])

#逗號可以去掉
plt.subplot(222)
plt.plot([0, 1], [0, 2])

plt.subplot(223)
plt.plot([0, 1], [0, 3])

plt.subplot(224)
plt.plot([0, 1], [0, 4])

plt.tight_layout()

# example 2:
###############################
plt.figure(figsize=(6, 4))
# plt.subplot(n_rows, n_cols, plot_num)
plt.subplot(2, 1, 1)
# figure splits into 2 rows, 1 col, plot to the 1st sub-fig
plt.plot([0, 1], [0, 1])

plt.subplot(234)
# figure splits into 2 rows, 3 col, plot to the 4th sub-fig
plt.plot([0, 1], [0, 2])

plt.subplot(235)
# figure splits into 2 rows, 3 col, plot to the 5th sub-fig
plt.plot([0, 1], [0, 3])

plt.subplot(236)
# figure splits into 2 rows, 3 col, plot to the 6th sub-fig
plt.plot([0, 1], [0, 4])

plt.tight_layout()
plt.show()

image

subplot常用方法

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

# method 1: subplot2grid
##########################
plt.figure()
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)  # stands for axes
ax1.plot([1, 2], [1, 2])
ax1.set_title('ax1_title')
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax4.scatter([1, 2], [2, 2])
ax4.set_xlabel('ax4_x')
ax4.set_ylabel('ax4_y')
ax5 = plt.subplot2grid((3, 3), (2, 1))

# method 2: gridspec
#########################
plt.figure()
gs = gridspec.GridSpec(3, 3)
# use index from 0
ax6 = plt.subplot(gs[0, :])
ax7 = plt.subplot(gs[1, :2])
ax8 = plt.subplot(gs[1:, 2])
ax9 = plt.subplot(gs[-1, 0])
ax10 = plt.subplot(gs[-1, -2])

# method 3: easy to define structure
####################################
f, ((ax11, ax12), (ax13, ax14)) = plt.subplots(2, 2, sharex=True, sharey=True)
ax11.scatter([1,2], [1,2])

plt.tight_layout()
plt.show()

image

Pin-Jiun commented 1 year ago

圖中圖

import matplotlib.pyplot as plt

fig = plt.figure()
x = [1, 2, 3, 4, 5, 6, 7]
y = [1, 3, 4, 2, 5, 8, 6]

# below are all percentage
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
ax1 = fig.add_axes([left, bottom, width, height])  # main axes
ax1.plot(x, y, 'r')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_title('title')

ax2 = fig.add_axes([0.2, 0.6, 0.25, 0.25])  # inside axes
ax2.plot(y, x, 'b')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_title('title inside 1')

# different method to add axes
####################################
plt.axes([0.6, 0.2, 0.25, 0.25])
plt.plot(y[::-1], x, 'g')
plt.xlabel('x')
plt.ylabel('y')
plt.title('title inside 2')

plt.show()

image

Pin-Jiun commented 1 year ago

主次座標軸

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 10, 0.1)
y1 = 0.05 * x**2
y2 = -1 *y1

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()    # mirror the ax1
ax1.plot(x, y1, 'g-')
ax2.plot(x, y2, 'b-')

ax1.set_xlabel('X data')
ax1.set_ylabel('Y1 data', color='g')
ax2.set_ylabel('Y2 data', color='b')

plt.show()

image

Pin-Jiun commented 1 year ago

動畫展示數據

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))

def animate(i):
    line.set_ydata(np.sin(x + i/10.0))  # update the data
    return line,

# Init only required for blitting to give a clean slate.
def init():
    line.set_ydata(np.sin(x))
    return line,

# call the animator.  blit=True means only re-draw the parts that have changed.
# blit=True dose not work on Mac, set blit=False
# interval= update frequency
ani = animation.FuncAnimation(fig=fig, func=animate, frames=100, init_func=init,
                              interval=20, blit=False)

# save the animation as an mp4.  This requires ffmpeg or mencoder to be
# installed.  The extra_args ensure that the x264 codec is used, so that
# the video can be embedded in html5.  You may need to adjust this for
# your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html
# anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

plt.show()
Pin-Jiun commented 1 year ago

What is the difference between np.linspace and np.arange?

np.arange(start, stop, step)

np.arange allows you to define the stepsize and infers the number of steps(the number of values you get).

np.linspace(start,stop,number)

np.linspace allows you to define how many values you get including the specified min and max value. It infers the stepsize:

Pin-Jiun commented 1 year ago

Save a Plot to a File Using Matplotlib

plt.savefig('file_name.xxx')

其中file_name可以是名稱或是絕對路徑path

matplotlib.pyplot.savefig(*args, **kwargs)

savefig(fname, *, dpi='figure', format=None, metadata=None,
        bbox_inches=None, pad_inches=0.1,
        facecolor='auto', edgecolor='auto',
        backend=None, **kwargs
       )

如果要用plt.savefig(), 一定要在plt.show()前, 因為plt.show()完會產生新的空白的圖