我试图了解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)
我将子图放在另一个图上,但我希望它们排成一行。
答案 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)