注册 登录
编程论坛 Python论坛

python时间轴问题

扬帆起航2 发布于 2019-01-31 14:19, 2085 次点击
平均几分钟写入一次记录数据,想利用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编辑过]

1 回复
#2
wei_ai_lu2019-02-02 14:39
没用过pandas,之前用过那啥,可以将坐标调倾斜,就不会有这样的效果了
1