使用 Pandas 在 Matplotlib 中设置 Y 轴

pandasmatplotlibserver side programmingprogramming更新于 2025/9/12 10:22:17

要使用 Pandas 在 Matplotlib 中设置 Y 轴,我们可以执行以下步骤 −

  • 创建一个字典,其键为 x 和 y。

  • 使用 Pandas 创建数据框。

  • 使用 Pandas plot 绘制数据点,其中 ylim(0, 25) 和 xlim(0, 15)。

  • 要显示图形,请使用 show() 方法。

示例

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
d = dict(
   x=np.linspace(0, 10, 10),
   y=np.linspace(0, 10, 10)*2
)
df = pd.DataFrame(d)
df.plot(kind="bar", ylim=(0, 25), xlim=(0, 15))
plt.show()

输出


相关文章