| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 101 人关注过本帖
标题:python时间轴问题
只看楼主 加入收藏
扬帆起航2
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2014-7-8
结帖率:0
  已结贴   问题点数:10  回复次数:1   
python时间轴问题
平均几分钟写入一次记录数据,想利用python做趋势,几年的数据,做出来如图:

X轴太多,加载在一起了,所以,想修改X轴坐标,结果报错:
Traceback (most recent call last):
  File "D:\share\d.py", line 26, in <module>
    plt.xticks(pd.date_range(new11.index[0],new11.index[-1],freq='M'),rotation=45)
  File "C:\Anaconda2\lib\site-packages\pandas\tseries\index.py", line 2051, in date_range
    closed=closed, **kwargs)
  File "C:\Anaconda2\lib\site-packages\pandas\util\decorators.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Anaconda2\lib\site-packages\pandas\tseries\index.py", line 267, in __new__
    ambiguous=ambiguous)
  File "C:\Anaconda2\lib\site-packages\pandas\tseries\index.py", line 427, in _generate
    start = Times**p(start)
  File "pandas\tslib.pyx", line 295, in pandas.tslib.Times**p.__new__ (pandas\tslib.c:9203)
  File "pandas\tslib.pyx", line 1343, in pandas.tslib.convert_to_tsobject (pandas\tslib.c:25854)
TypeError: Cannot convert input to Times**p

代码如下:
程序代码:

#!/usr/local/miniconda2/envs/science/bin/python
#
-*- coding: utf-8 -*-
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdate
import MySQLdb

db=MySQLdb.connect(host="127.0.0.1",user='root',port=3306,db="test")
sql2016="SELECT * FROM (SELECT DATE_FORMAT(Timee,'%Y-%m-%d %H:%i') AS riqi,PH AS 2016ph FROM table1 ) t WHERE t.riqi = 2016"
sql2017="SELECT * FROM (SELECT DATE_FORMAT(Timee,'%Y-%m-%d %H:%i') AS riqi,PH AS 2017ph FROM table1 ) t WHERE t.riqi = 2017"
df2017=pd.read_sql(sql2017,con=db)
df2016=pd.read_sql(sql2016,con=db)

new=pd.concat([df2016,df2017],axis=1)
new_df=new[["riqi","2016ph","2017ph"]]
new11=new_df.set_index("riqi")

fig = plt.figure()
ax = fig.add_subplot(111)
ax.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d'))


plt.xticks(pd.date_range(new11.index[0],new11.index[-1],freq='M'),rotation=45)
ax.plot(new11.index,new11['2016ph','2017ph'],color='r')
new11.plot()
plt.show()

其中new11中值如下:

想问怎么修改,能实现,X轴以月为单位,谢谢!



[此贴子已经被作者于2019-1-31 14:20编辑过]

附件: 您没有浏览附件的权限,请 登录注册
2019-01-31 14:19
wei_ai_lu
Rank: 2
等 级:论坛游民
威 望:2
帖 子:14
专家分:98
注 册:2018-5-30
  得分:10 
没用过pandas,之前用过那啥,可以将坐标调倾斜,就不会有这样的效果了
2019-02-02 14:39







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045084 second(s), 8 queries.
Copyright©2004-2019, BCCN.NET, All Rights Reserved