找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 522|回复: 0

[求助] python时间轴问题

1

主题

1

帖子

1

积分

贫民

积分
1
pyupup 发表于 2019-1-31 14:08:56 | 显示全部楼层 |阅读模式
平均几分钟写入一次记录数据,想利用python做趋势,几年的数据,做出来如图:
figure_1.png
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


代码如下:
  1. #!/usr/local/miniconda2/envs/science/bin/python
  2. #-*- coding: utf-8 -*-
  3. import pandas as pd
  4. import matplotlib.pyplot as plt
  5. import matplotlib.dates as mdate
  6. import MySQLdb

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

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

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


  18. plt.xticks(pd.date_range(new11.index[0],new11.index[-1],freq='M'),rotation=45)
  19. ax.plot(new11.index,new11['2016ph','2017ph'],color='r')
  20. new11.plot()
  21. plt.show()
复制代码

其中new11中值如下:
1.PNG
想问怎么修改,能实现,X轴以月为单位,谢谢!


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表