【问题标题】:How to make my DataFrame.plot subplots posted in line?如何使我的 DataFrame.plot 子图在线发布?
【发布时间】:2019-08-26 22:39:52
【问题描述】:

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

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

【问题讨论】:

  • 这么说,您的意思是它们当前对齐为一列多行,但您希望它们为一列多列?
  • 没错,我希望这两个图在一行和两列中。

标签: python pandas matplotlib plot subplot


【解决方案1】:

你可以从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)

【讨论】:

  • 谢谢,但不幸的是,它们仍然比另一个高。我什至将您的代码粘贴到一个新单元格,但我仍然得到相同的结果
【解决方案2】:

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

【讨论】:

  • 谢谢你,gmds,我不明白为什么,但第一个选项不起作用,但第二个有效!
猜你喜欢
  • 2017-08-22
  • 2023-03-11
  • 2014-03-24
  • 2023-03-28
  • 2013-05-04
  • 2016-06-08
  • 2017-11-11
  • 2014-12-17
  • 1970-01-01
相关资源
最近更新 更多