1

I am trying to create a PNG image with some CSV data but I am getting an error related to the date column (meanwhile converted to list). The error is:

Traceback (most recent call last):
File "C:/Users/user1/Desktop/Py/AgentsStatus/testGraph.py", line 57, in <module>
    plt.plot(dateCol,okCol,linewidth=5)
File "C:\Python34\lib\site-packages\matplotlib\pyplot.py", line 3154, in plot
    ret = ax.plot(*args, **kwargs)
File "C:\Python34\lib\site-packages\matplotlib\__init__.py", line 1812, in inner
    return func(ax, *args, **kwargs)
File "C:\Python34\lib\site-packages\matplotlib\axes\_axes.py", line 1425, in plot
    self.add_line(line)
File "C:\Python34\lib\site-packages\matplotlib\axes\_base.py", line 1708, in add_line
    self._update_line_limits(line)
File "C:\Python34\lib\site-packages\matplotlib\axes\_base.py", line 1730, in _update_line_limits
    path = line.get_path()
File "C:\Python34\lib\site-packages\matplotlib\lines.py", line 925, in get_path
    self.recache()
File "C:\Python34\lib\site-packages\matplotlib\lines.py", line 612, in recache
    x = np.asarray(xconv, np.float_)
File "C:\Python34\lib\site-packages\numpy\core\numeric.py", line 482, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: '11-04-2016'

CSV contains:

11-04-2016;37180;6;23852
18-04-2016;37341;9;24105
25-04-2016;37075;18;23788

My code is:

import csv
import matplotlib.pyplot as plt
import os

path = 'C:\\Users\\user1\\Desktop\\Py\\AgentsStatus\\data.csv'

with open (path) as csvfile:
    readCSV = csv.reader(csvfile, delimiter=';')

    dateCol = [] # date list

    for row in readCSV:
        if row:
            date0 = row[0]
            dateCol.append(date0)

with open (path) as csvfile:
    readCSV = csv.reader(csvfile, delimiter=';')

    okCol = [] # all agents list

    for row in readCSV:
        if row:
            ok0 = row[1]
            okCol.append(ok0)


with open (path) as csvfile:
    readCSV = csv.reader(csvfile, delimiter=';')

    heaCol = [] # healthy list




from matplotlib import pyplot as plt
from matplotlib import style

style.use('ggplot')


# can plot specifically, after just showing the defaults:
plt.plot(dateCol,okCol,linewidth=5)
plt.plot(dateCol,heaCol,linewidth=5)

plt.title('Epic Info')
plt.ylabel('Y axis')
plt.xlabel('X axis')

plt.show()

My goal is to create something like below:

enter image description here

Could you give me please some tips of what am I doing wrong? I think my problem mitght be that I am not setting the List dateCol as the Index for the graph (to not plot it). Could you please help me on that?

Thanks a lot.

9
  • I am getting an error related to the date column - What is the error? Commented Jul 13, 2016 at 14:16
  • Hello, its ValueError: could not convert string to float: '11-04-2016' Commented Jul 13, 2016 at 14:17
  • 1
    That's an important piece of information - include the entire traceback in your question. Commented Jul 13, 2016 at 14:19
  • Already updated the question. Thanks! Commented Jul 13, 2016 at 14:20
  • Your problem may go a little deeper than just that error. Have you looked at this recipe? matplotlib.org/examples/api/date_demo.html Commented Jul 13, 2016 at 14:21

1 Answer 1

2

For stuff like this Pandas is unbeatable:

import pandas
import matplotlib.pyplot as plt

df = pandas.read_csv('sampledata.csv', delimiter=';', 
                     index_col=0, 
                     parse_dates=[0], dayfirst=True, 
                     names=['date','a','b','c'])
df.plot()

plt.savefig('sampledata.png')

Results in

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. It's what I was looking for. Just one question, where can I find information to configure for example colors of lines, write dates in vertical (for bigger sets of data), etc..?
The docs are pretty good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.