numpy - Python matplotlib 畫(huà)直方圖出錯(cuò)?
問(wèn)題描述
sql3 = ’select sum(comment_num) as total_col,create_time from article GROUP BY create_time’df = pd.read_sql(sql3, conn)print(df)# 總數(shù)# N = 22# 寬度width = 0.45# ind = np.arange(N)plt.bar(df[’create_time’], df[’total_col’], width, color=’r’, label=’total_col’)plt.xlabel(u'發(fā)表日期')plt.ylabel(u'總評(píng)論數(shù)')plt.title(u'每日發(fā)表文章的總評(píng)論數(shù)直方分布圖')plt.legend()plt.show()
df:
total_col create_time0 2.0 2017-04-271 0.0 2017-05-092 3.0 2017-05-103 6.0 2017-05-114 3.0 2017-05-125 2.0 2017-05-136 1.0 2017-05-147 0.0 2017-05-158 5.0 2017-05-169 0.0 2017-05-17101.0 2017-05-18110.0 2017-05-19126.0 2017-05-22130.0 2017-05-24141.0 2017-05-25150.0 2017-05-26166.0 2017-05-27174.0 2017-05-2918 16.0 2017-05-31194.0 2017-06-02202.0 2017-06-04211.0 2017-06-05
錯(cuò)誤:
Traceback (most recent call last): File 'D:/PyCharm/py_scrapyjobbole/data_analysis.py', line 46, in <module> plt.bar(df[’create_time’], df[’total_col’], width, color=’r’, label=’total_col’) File 'D:python-3.5.2libsite-packagesmatplotlibpyplot.py', line 2704, in bar **kwargs) File 'D:python-3.5.2libsite-packagesmatplotlib__init__.py', line 1898, in inner return func(ax, *args, **kwargs) File 'D:python-3.5.2libsite-packagesmatplotlibaxes_axes.py', line 2105, in bar left = [left[i] - width[i] / 2. for i in xrange(len(left))] File 'D:python-3.5.2libsite-packagesmatplotlibaxes_axes.py', line 2105, in <listcomp> left = [left[i] - width[i] / 2. for i in xrange(len(left))]TypeError: unsupported operand type(s) for -: ’datetime.date’ and ’float’
問(wèn)題解答
回答1:試試astype()轉(zhuǎn)換型別,參見(jiàn)stackoverflow
%matplotlib inlineimport pandas as pddf = pd.DataFrame.from_csv(’timeseries.tsv’, sep='t')df[’total_col’] = df[’total_col’].astype(float)df[’create_time’] = df[’create_time’].astype(’datetime64[D]’)df.set_index([’create_time’]).plot(kind=’bar’)
plt.bar(df[’create_time’], df[’total_col’], width, color=’r’, label=’total_col’)
里面的left, height參數(shù)應(yīng)該是數(shù)值形的list,你現(xiàn)在df[’create_time’]傳遞的是時(shí)間類(lèi)型的列表
相關(guān)文章:
1. javascript - npm下載的模塊不完整是什么問(wèn)題?2. java - Spring事務(wù)回滾問(wèn)題3. apache - 本地搭建wordpress權(quán)限問(wèn)題4. c++ - 如何在python的阻塞的函數(shù)中獲取變量值5. node.js - 我想讓最后進(jìn)入數(shù)據(jù)庫(kù)的數(shù)據(jù),在前臺(tái)最先展示,如何做到?6. wordpress - Nginx中禁止訪(fǎng)問(wèn)txt,robots.txt文件例外,規(guī)則該怎么寫(xiě)?7. 剛放到服務(wù)器的項(xiàng)目出現(xiàn)這中錯(cuò)誤,有高手指點(diǎn)嗎8. python - django 按日歸檔統(tǒng)計(jì)訂單求解9. python 操作mysql如何經(jīng)量防止自己的程序在之后被惡意注入(說(shuō)白了就是問(wèn)一下python防注入的一些要點(diǎn))10. mysql - 面試題:如何把login_log表轉(zhuǎn)換成last_login表?
