# 如何在 Matplotlib 中繪製對數軸

Suraj Joshi 2023年1月30日

## `set_xscale()` 或 `set_yscale()` 函式

``````import pandas as pd
import matplotlib.pyplot as plt

date = ["28 April", "27 April", "26 April", "25 April", "24 April", "23 April"]

revenue = [2954222, 2878196, 2804796, 2719896, 2626321, 2544792]

company_data_df = pd.DataFrame({"date": date, "total_revenue": revenue})
company_data = company_data_df.sort_values(by=["total_revenue"])
fig = plt.figure(figsize=(8, 6))
plt.scatter(company_data["total_revenue"], company_data["date"])
plt.plot(company_data["total_revenue"], company_data["date"])
plt.xscale("log")
plt.xlabel("Total Revenue")
plt.ylabel("Date")
plt.title("Company Growth", fontsize=25)
plt.show()
``````

``````import pandas as pd
import matplotlib.pyplot as plt

date = ["28 April", "27 April", "26 April", "25 April", "24 April", "23 April"]

revenue = [2954222, 2878196, 2804796, 2719896, 2626321, 2544792]

company_data_df = pd.DataFrame({"date": date, "total_revenue": revenue})
company_data = company_data_df.sort_values(by=["total_revenue"])
fig = plt.figure(figsize=(8, 6))
plt.scatter(company_data["date"], company_data["total_revenue"])
plt.plot(company_data["date"], company_data["total_revenue"])
plt.yscale("log")
plt.xlabel("Date")
plt.ylabel("Total Revenue")
plt.title("Company Growth", fontsize=25)
plt.show()
``````

``````import pandas as pd
import matplotlib.pyplot as plt

x = [10, 100, 1000, 10000, 100000]
y = [2, 4, 8, 16, 32]

fig = plt.figure(figsize=(8, 6))
plt.scatter(x, y)
plt.plot(x, y)
plt.grid()
plt.xscale("log")
plt.yscale("log", basey=2)
plt.xlabel("x", fontsize=20)
plt.ylabel("y", fontsize=20)
plt.title("Plot with both log axes", fontsize=25)
plt.show()
``````

## `semilogx()` 或 `semilogy()` 函式

`semilogx()` 函式建立沿 X 軸具有對數縮放比例的圖，而 `semilogy()` 函式建立沿 Y 軸具有對數縮放比例的圖。預設的對數底數是 10，而底數可以分別為函式 `semilogx()``semilogy()` 設定 `basex``basey` 引數。

``````import pandas as pd
import matplotlib.pyplot as plt

date = ["28 April", "27 April", "26 April", "25 April", "24 April", "23 April"]

revenue = [2954222, 2878196, 2804796, 2719896, 2626321, 2544792]

company_data_df = pd.DataFrame({"date": date, "total_revenue": revenue})
company_data = company_data_df.sort_values(by=["total_revenue"])
fig = plt.figure(figsize=(8, 6))
plt.scatter(company_data["total_revenue"], company_data["date"])
plt.plot(company_data["total_revenue"], company_data["date"])
plt.semilogx()
plt.xlabel("Total Revenue")
plt.ylabel("Date")
plt.title("Company Growth", fontsize=25)
plt.show()
``````

``````import pandas as pd
import matplotlib.pyplot as plt

x = [10, 100, 1000, 10000, 100000]
y = [2, 4, 8, 16, 32]

fig = plt.figure(figsize=(8, 6))
plt.scatter(x, y)
plt.plot(x, y)
plt.grid()
plt.semilogx()
plt.semilogy(basey=2)
plt.xlabel("x", fontsize=20)
plt.ylabel("y", fontsize=20)
plt.title("Plot with both log axes", fontsize=25)
plt.show()
``````

## `loglog()` 函式

``````import pandas as pd
import matplotlib.pyplot as plt

x = [10, 100, 1000, 10000, 100000]
y = [2, 4, 8, 16, 32]

fig = plt.figure(figsize=(8, 6))
plt.scatter(x, y)
plt.plot(x, y)
plt.loglog(basex=10, basey=2)
plt.xlabel("x", fontsize=20)
plt.ylabel("y", fontsize=20)
plt.title("Plot with both log axes", fontsize=25)
plt.show()
``````

Suraj Joshi is a backend software engineer at Matrice.ai.