如何让我的 DataFrame.plot 子图成行发布?

How to make my DataFrame.plot subplots posted in line?

我试图了解 pandas.DataFrame.plot 的工作原理,但坚持将多个子图排成一行。我感到很困惑,所以我的问题可能听起来很奇怪。但我将不胜感激。

recent_grads.plot(x = "Women", y = "Median", kind = "scatter", subplots = True, figsize=(6, 6), layout = (2,2), sharex = False)
recent_grads.plot(x = "Men", y = "Median", kind = "scatter", subplots = True, figsize=(6, 6), layout = (2,2), sharex = False)

我正在将我的子图放在另一个下面,但我希望它们排成一行。

您可以使用 matplotlib.pyplot

中的 plt.subplots
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=1, ncols=2)
fig.set_size_inches(6, 6)
plt.subplots_adjust(wspace=0.2)
recent_grads.plot(x = "Women", y = "Median", kind = "scatter", ax=ax[0], sharex = False)
recent_grads.plot(x = "Men", y = "Median", kind = "scatter", ax=ax[1], sharex = False)

你只需要修改layout:

recent_grads.plot(x = "Women", y = "Median", kind = "scatter", subplots = True, figsize=(6, 6), layout = (1, 1), sharex = False)
recent_grads.plot(x = "Men", y = "Median", kind = "scatter", subplots = True, figsize=(6, 6), layout = (1, 2), sharex = False)

另一种方法是创建 Axes 对象并明确指定它们:

from matplotlib import pyplot as plt

fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(6, 6))

ax1, ax2 = axes

recent_grads.plot(x = "Women", y = "Median", kind = "scatter", ax=ax1)
recent_grads.plot(x = "Men", y = "Median", kind = "scatter", ax=ax2)