如何使我的DataFrame.plot子图在线发布?

时间:2019-04-05 04:44:03

标签: python pandas matplotlib plot subplot

我试图了解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)

我将子图放在另一个图上,但我希望它们排成一行。

2 个答案:

答案 0 :(得分:1)

u可以使用plt.subplots中的matplotlib.pyplot

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)

答案 1 :(得分:1)

您只需要修改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)