遵循本教程使用matplotlib和pandas绘制图形会导致错误

时间:2019-05-19 15:51:25

标签: python pandas python-2.7 matplotlib

因此,我从头开始学习本教程,并遇到了问题。 我正在尝试绘制图形,但是出了点问题

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from reader import df_final

crimes_count_date = df_final.pivot_table('ID', aggfunc=np.size, columns='Primary Type',
                                         index=df_final.index.date, fill_value=0)
crimes_count_date.index = pd.DatetimeIndex(crimes_count_date.index)
plot1 = crimes_count_date.rolling(365).sum().plot(figsize=(12, 30), subplots=True,
                                                 layout=(-1, 3), sharex=False, sharey=False)
plt.plot(plot1)
plt.show()

好吧,df_final是从csv文件读取的最终数据帧

我运行print(type(plot1))时说<type 'numpy.ndarray'>

错误日志显示

  

回溯(最近通话最近):     在第27行的“ /Users/ily​​a/PycharmProjects/coursework/trends.py”文件中       plot_frequency_trends()     在plot_frequency_trends中,文件“ /Users/ily​​a/PycharmProjects/coursework/trends.py”,第21行       plt.plot(plot1)     在图中绘制文件“ /Users/ily​​a/PycharmProjects/coursework/venv/lib/python2.7/site-packages/matplotlib/pyplot.py”,行3363       ret = ax.plot(* args,** kwargs)     在内部的文件“ /Users/ily​​a/PycharmProjects/coursework/venv/lib/python2.7/site-packages/matplotlib/init.py”中,       return func(ax,* args,** kwargs)     在图中绘制文件“ /Users/ily​​a/PycharmProjects/coursework/venv/lib/python2.7/site-packages/matplotlib/axes/_axes.py”,第1529行       self.add_line(行)     文件“ /Users/ily​​a/PycharmProjects/coursework/venv/lib/python2.7/site-packages/matplotlib/axes/_base.py”,行1960,在add_line中       self._update_line_limits(行)     文件“ /Users/ily​​a/PycharmProjects/coursework/venv/lib/python2.7/site-packages/matplotlib/axes/_base.py”,行1982,在_update_line_limits中       路径= line.get_path()     在get_path的第956行,文件“ /Users/ily​​a/PycharmProjects/coursework/venv/lib/python2.7/site-packages/matplotlib/lines.py”       self.recache()     重新缓存文件“ /Users/ily​​a/PycharmProjects/coursework/venv/lib/python2.7/site-packages/matplotlib/lines.py”,行657       y = _to_unmasked_float_array(yconv).ravel()     文件“ /Users/ily​​a/PycharmProjects/coursework/venv/lib/python2.7/site-packages/matplotlib/cbook/init.py”,第205行,位于_to_unmasked_float_array       返回np.asarray(x,float)     文件“ /Users/ily​​a/PycharmProjects/coursework/venv/lib/python2.7/site-packages/numpy/core/numeric.py”,行538,以数组形式显示       返回数组(a,dtype,copy = False,order = order)   TypeError:float()参数必须是字符串或数字

感谢您的帮助!

0 个答案:

没有答案