Python如何繪製Major Minor Grid(主要/次要格線)? import matplotlib.pyplot as plt; fig, ax = plt.subplots(1,1) ; plt.minorticks_on() ; ax.grid(visible=True, which=”major”, c=”k”, linewidth=1) ; ax.grid(visible=True, which=”minor”, c=”k”, linewidth=0.5)

加入好友
加入社群
Python如何繪製Major Minor Grid(主要/次要格線)? import matplotlib.pyplot as plt; fig, ax = plt.subplots(1,1) ; plt.minorticks_on() ; ax.grid(visible=True, which="major", c="k", linewidth=1) ; ax.grid(visible=True, which="minor", c="k", linewidth=0.5) - 儲蓄保險王

參考資料

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sbn

PI = np.pi
x = np.linspace(0,2*PI,100)
y = np.sin(x)

fig, ax = plt.subplots(1,1) #subplots()可以參考這裡

#這行可改為: ax=plt.axes()

ax.scatter(x,y)

ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator() )
ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator() )
ax.grid(b=True,which=”major”,c=”k”,linewidth=1.0)
ax.grid(b=True,which=”minor”,c=”k”,linewidth=0.5)

Python如何繪製Major Minor Grid(主要/次要格線)? import matplotlib.pyplot as plt; fig, ax = plt.subplots(1,1) ; plt.minorticks_on() ; ax.grid(visible=True, which="major", c="k", linewidth=1) ; ax.grid(visible=True, which="minor", c="k", linewidth=0.5) - 儲蓄保險王

VS code不能省略最後一行:

plt.show() 或

fig.show()

 

使用Spyder,

省略最後一行的.show()

仍可以正常輸出結果:

Python如何繪製Major Minor Grid(主要/次要格線)? import matplotlib.pyplot as plt; fig, ax = plt.subplots(1,1) ; plt.minorticks_on() ; ax.grid(visible=True, which="major", c="k", linewidth=1) ; ax.grid(visible=True, which="minor", c="k", linewidth=0.5) - 儲蓄保險王

 

matplotlib官網:

Python如何繪製Major Minor Grid(主要/次要格線)? import matplotlib.pyplot as plt; fig, ax = plt.subplots(1,1) ; plt.minorticks_on() ; ax.grid(visible=True, which="major", c="k", linewidth=1) ; ax.grid(visible=True, which="minor", c="k", linewidth=0.5) - 儲蓄保險王

Figure, Axes基本觀念也可參考此連結

 

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
fpath = r”C:\22056 EPP IL\IL.xlsx”
df = pd.read_excel(fpath,header=None)
df_ILvsFreq = df.iloc[:,[7,8]].reset_index(drop=True)
df_ILvsFreq_drop = df_ILvsFreq.dropna(axis=0,how=”all”).reset_index(drop=True)
df_data = df_ILvsFreq_drop.iloc[1:,:].reset_index(drop=True)
dfx = df_data[7]
dfy = df_data[8]

fig,ax = plt.subplots()
ax.plot(dfx,dfy) #x,y放Series也可以畫
ax.set_title(“Insertion Loss vs Frequency”)
ax.set_xlabel(“Frequency(GHZ)”)
ax.set_ylabel(“Insertion Loss(dB)”)
ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator() )
ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator() )
ax.grid(visible=True,which=”major”,c=”k”,linewidth=1)
ax.grid(visible=True,which=”minor”,c=”k”,linewidth=0.5)
fig.savefig(r”C:\22056 EPP IL\ILvsFreq”)

Python如何繪製Major Minor Grid(主要/次要格線)? import matplotlib.pyplot as plt; fig, ax = plt.subplots(1,1) ; plt.minorticks_on() ; ax.grid(visible=True, which="major", c="k", linewidth=1) ; ax.grid(visible=True, which="minor", c="k", linewidth=0.5) - 儲蓄保險王

 

# ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator() )
# ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator() )

這兩行程式碼很長

有無替代方式?

參考stack overflow

plt.minorticks_on()

#ax.minorticks_on() #也可

Python如何繪製Major Minor Grid(主要/次要格線)? import matplotlib.pyplot as plt; fig, ax = plt.subplots(1,1) ; plt.minorticks_on() ; ax.grid(visible=True, which="major", c="k", linewidth=1) ; ax.grid(visible=True, which="minor", c="k", linewidth=0.5) - 儲蓄保險王

 

推薦hahow線上學習python: https://igrape.net/30afN

加入好友
加入社群
Python如何繪製Major Minor Grid(主要/次要格線)? import matplotlib.pyplot as plt; fig, ax = plt.subplots(1,1) ; plt.minorticks_on() ; ax.grid(visible=True, which="major", c="k", linewidth=1) ; ax.grid(visible=True, which="minor", c="k", linewidth=0.5) - 儲蓄保險王

儲蓄保險王

儲蓄險是板主最喜愛的儲蓄工具,最喜愛的投資理財工具則是ETF,最喜愛的省錢工具則是信用卡

You may also like...

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *