Python 使用pandas中的plot方法在一行中绘制图形的问题

Python 使用pandas中的plot方法在一行中绘制图形的问题,python,pandas,matplotlib,plot,subplot,Python,Pandas,Matplotlib,Plot,Subplot,假设我想在一行中绘制3个图形:其他3个特性的依赖项cnt 代码: fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 10)) for idx, feature in enumerate(min_regressors): df_shuffled.plot(feature, "cnt", subplots=True, kind="scatter", ax= axes[0, idx]) plt.show() 错误消息: Index

假设我想在一行中绘制3个图形:其他3个特性的依赖项
cnt

代码:

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 10))
for idx, feature in enumerate(min_regressors):
    df_shuffled.plot(feature, "cnt", subplots=True, kind="scatter", ax= axes[0, idx])
plt.show()
错误消息:

IndexErrorTraceback (most recent call last)
<ipython-input-697-e15bcbeccfad> in <module>()
      2 fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 10))
      3 for idx, feature in enumerate(min_regressors):
----> 4     df_shuffled.plot(feature, "cnt", subplots=True, kind="scatter", ax= axes[0, idx])
      5 plt.show()

IndexError: too many indices for array
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(15, 10))
for idx, feature in enumerate(min_regressors):
    df_shuffled.plot(feature, "cnt", subplots=True, kind="scatter", ax= axes[idx / 2, idx % 2])
plt.show()
输出:

IndexErrorTraceback (most recent call last)
<ipython-input-697-e15bcbeccfad> in <module>()
      2 fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 10))
      3 for idx, feature in enumerate(min_regressors):
----> 4     df_shuffled.plot(feature, "cnt", subplots=True, kind="scatter", ax= axes[0, idx])
      5 plt.show()

IndexError: too many indices for array
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(15, 10))
for idx, feature in enumerate(min_regressors):
    df_shuffled.plot(feature, "cnt", subplots=True, kind="scatter", ax= axes[idx / 2, idx % 2])
plt.show()


我正在使用python 2.7

这个问题与熊猫无关。您看到的索引错误来自
ax=axes[0,idx]
。这是因为您只有一行<代码>[0,idx]在您有多行时可以工作

对于一行,您可以跳过第一个索引并使用

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 10))
for idx, feature in enumerate(min_regressors):
    df_shuffled.plot(feature, "cnt", subplots=True, kind="scatter", ax= axes[idx])
plt.show()
概述

正确

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))
axes[0].plot([1,2], [1,2])
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))
axes[0, 0].plot([1,2], [1,2])
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(8, 3))
axes[0,0].plot([1,2], [1,2])
不正确

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))
axes[0].plot([1,2], [1,2])
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))
axes[0, 0].plot([1,2], [1,2])
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(8, 3))
axes[0,0].plot([1,2], [1,2])
正确

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))
axes[0].plot([1,2], [1,2])
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))
axes[0, 0].plot([1,2], [1,2])
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(8, 3))
axes[0,0].plot([1,2], [1,2])

这个问题与熊猫无关。您看到的索引错误来自
ax=axes[0,idx]
。这是因为您只有一行<代码>[0,idx]在您有多行时可以工作

对于一行,您可以跳过第一个索引并使用

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 10))
for idx, feature in enumerate(min_regressors):
    df_shuffled.plot(feature, "cnt", subplots=True, kind="scatter", ax= axes[idx])
plt.show()
概述

正确

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))
axes[0].plot([1,2], [1,2])
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))
axes[0, 0].plot([1,2], [1,2])
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(8, 3))
axes[0,0].plot([1,2], [1,2])
不正确

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))
axes[0].plot([1,2], [1,2])
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))
axes[0, 0].plot([1,2], [1,2])
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(8, 3))
axes[0,0].plot([1,2], [1,2])
正确

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))
axes[0].plot([1,2], [1,2])
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))
axes[0, 0].plot([1,2], [1,2])
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(8, 3))
axes[0,0].plot([1,2], [1,2])

为了让您了解和理解正在发生的事情,我建议您检查这两种情况下
轴的大小。您将看到,当
nrows
ncols
为1时,轴变量将是一维的,否则它将是二维的

您无法按当前方式为一维对象编制索引(
ax=axes[0,idx]

您可以使用numpy将轴设置为二维

或者,更好的解决方案是直接迭代特征和轴:

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 10))
for ax, feature in zip(axes, min_regressors):
    df_shuffled.plot(feature, "cnt", subplots=True, kind="scatter", ax=ax)
plt.show()

为了让您了解和理解正在发生的事情,我建议您检查这两种情况下
轴的大小。您将看到,当
nrows
ncols
为1时,轴变量将是一维的,否则它将是二维的

您无法按当前方式为一维对象编制索引(
ax=axes[0,idx]

您可以使用numpy将轴设置为二维

或者,更好的解决方案是直接迭代特征和轴:

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 10))
for ax, feature in zip(axes, min_regressors):
    df_shuffled.plot(feature, "cnt", subplots=True, kind="scatter", ax=ax)
plt.show()