简体   繁体   English

Python matplotlib分类数据y轴和date :: time x轴

[英]Python matplotlib categorical data y-axis and date::time x-axis

So my problem is that i'm developing a software for displaying some data, in this situation it's temperature along the y-axis and time along the x-axis which works fine. 所以我的问题是我正在开发用于显示一些数据的软件,在这种情况下,y轴的温度和x轴的时间都可以正常工作。 Though i want to display the temperature in labels as 21-22 and 22-23 and so on, on the y-axis but i get this error. 虽然我想在标签上显示温度为21-22和22-23等,但在y轴上却显示此错误。

Traceback (most recent call last):
  File "/home/seb/Documents/Bachelor/gui.py", line 271, in plot
    axes.plot(df.index.values, df['readings'], 'r-', linewidth=1, linestyle='-', color='#E9B955')
  File "/home/seb/.local/lib/python3.5/site-packages/matplotlib/__init__.py", line 1845, in inner
    return func(ax, *args, **kwargs)
  File "/home/seb/.local/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 1525, in plot
    for line in self._get_lines(*args, **kwargs):
  File "/home/seb/.local/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 406, in _grab_next_args
    for seg in self._plot_args(this, kwargs):
  File "/home/seb/.local/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 391, in _plot_args
    ncx, ncy = x.shape[1], y.shape[1]
IndexError: tuple index out of range

My data looks something like this 我的数据看起来像这样

                      readings
timestamp
2016-09-05 12:00:00  22.4-24.1
2016-09-05 13:00:00  20.6-22.4
2016-09-05 15:00:00  22.4-24.1
2016-09-05 16:00:00  22.4-24.1
2016-09-05 18:00:00  22.4-24.1
2016-09-05 19:00:00  22.4-24.1
2016-09-05 20:00:00  22.4-24.1
2016-09-05 21:00:00  22.4-24.1
2016-09-05 23:00:00  20.6-22.4

I can easily plot it when it's not in intervals. 当它不在间隔中时,我可以轻松地绘制它。 So my question is, what can i do to the data like this? 所以我的问题是,我该如何处理此类数据? I'm using matplotlib and pandas dataframes if that's any help. 如果有任何帮助,我正在使用matplotlib和pandas数据框。

So after a lot trouble concerning this, it was actually quite an easy "fix" The dataframe has a method called .astype() which lets you choose what kind of type the data in the perticular column should be. 因此,在对此进行了很多麻烦之后,实际上这是一个非常简单的“修复”。数据框具有一个称为.astype()的方法,该方法使您可以选择垂直列中的数据应为哪种类型。 For this problem, it just had to be changed to string. 对于此问题,只需将其更改为字符串即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM