# 如何在 Matplotlib 中绘制对数轴

Suraj Joshi 2024年2月15日

## `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.