2016-07-13 12 views
1

Ich versuche, ein PNG-Bild mit einigen CSV-Daten zu erstellen, aber ich bekomme einen Fehler in Bezug auf die Datumsspalte (mittlerweile in Liste konvertiert). Der Fehler ist:Convert CSV in PNG mit Matplotlib Problem

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 enthält:

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

Mein Code ist:

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() 

Mein Ziel ist es so etwas wie unten zu erstellen:

enter image description here

Könntest du mir bitte ein paar Tipps geben, was mache ich falsch? Ich denke mein Problem ist, dass ich die List dateCol nicht als Index für das Diagramm einstelle (um es nicht zu plotten). Könnten Sie mir bitte dabei helfen?

Vielen Dank.

+0

'Ich erhalte eine Fehlermeldung zu dem Zeitpunkt column' in Verbindung stehend - Was ist der Fehler? – That1Guy

+0

Hallo, sein ValueError: Konnte String in Float nicht konvertieren: '11 -04-2016 ' – Gonzalo

+1

Das ist eine wichtige Information - schließen Sie den * gesamten Traceback * in Ihre Frage ein. – That1Guy

Antwort

1

für Sachen wie diese Pandas ist unschlagbar:

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') 

Ergebnisse in

enter image description here

+0

Danke. Es ist, was ich gesucht habe. Nur eine Frage, wo kann ich Informationen finden, um zum Beispiel Farben von Linien zu konfigurieren, Daten in vertikale (für größere Datenmengen) zu schreiben, etc ..? – Gonzalo

+1

[Die Dokumente] (http://pandas.pydata.org/pandas-docs/stable/visualization.html) sind ziemlich gut. – chthonicdaemon