首页 > 解决方案 > 构建子图

问题描述

我正在尝试创建一系列子图:

count=0
fig1, axes1 = plt.subplots(nrows=2, ncols=1, figsize=(10,80))
for x in b:
   """code gets data here as a dataframe"""
   axes1[count]=q1.plot()
   count=count+1

但是,这会在一个图中创建两个图而不是 2 个子图。我在 Pycharm 中使用 python 3.5。一直在为我在这里做错的事情绞尽脑汁

标签: pythondataframematplotlib

解决方案


您正在使用创建一个轴数组(子图)fig1, axes1 = plt.subplots(...)。然后,当您这样做时,您将覆盖此数组的元素

axes1[count]=q1.plot()

q1.plot()您被分配给该数组的一个元素的返回值。

axes1[count].plot(...)如果您使用的是纯 matplotlib,您可能想要做的是使用您的轴直接绘图。当您使用 pandas 时,将您的坐标区作为参数传递给绘图函数:

count=0
fig1, axes1 = plt.subplots(nrows=2, ncols=1, figsize=(10,80))
for x in b:
   """code gets data here as a dataframe"""
   q1.plot(ax=axes1[count])
   count=count+1

推荐阅读