首页 > 解决方案 > 在 matplotlib 中绘制数据框数据

问题描述

我有以下代码用于在一个图中绘制两个数据帧数据。

import pandas as pd
import numpy as np
from pprint import pprint
from matplotlib import pyplot as plt


df1 = pd.DataFrame(np.random.randn(10, 10))
df2 = pd.DataFrame(np.random.randn(10, 10))


plt.figure()
fig, axes = plt.subplots(nrows=1, ncols=1, squeeze=False)

df1.plot(ax=axes[0], style='o-')
axes[0].set_xlabel('x')
axes[0].set_ylabel('y')
axes[0].set_title('ttl')

df2.plot(ax=axes[0], style='--')
axes[0].set_xlabel('x')
axes[0].set_ylabel('y')
axes[0].set_title('ttl')

但是,我收到以下错误

fig = self.ax.get_figure()
AttributeError: 'numpy.ndarray' object has no attribute 'get_figure'

有关如何解决此问题的任何建议都将非常有帮助。

标签: python-3.xdataframematplotlib

解决方案


get_figure是一种方法Axis,但是,self.axndarray根据错误。由于 的名称ax,假设它是一个Axis对象数组是合理的,因此您将需要调用get_figure数组项,而不是实际的数组。


推荐阅读