Seite 1 von 1

matplotlib problem

Verfasst: Mittwoch 18. August 2010, 09:12
von mit
Hallo,
mir gefällt dieses Beispiel ( http://matplotlib.sourceforge.net/examp ... atter.html ), d.h. dass jeder Datenpunkt im Graphen markiert ist. Leider bekomme ich beim ausführen diese Fehlermeldung:

Code: Alles auswählen

$ python date_index_formatter.py
Traceback (most recent call last):
  File "date_index_formatter.py", line 16, in <module>
    datafile = cbook.get_sample_data('msft.csv', asfileobj=False)
AttributeError: 'module' object has no attribute 'get_sample_data'
mit Matplotlib 0.99.1.2. Ein anders Problem ist, dass die Eingabedatei 'msft.csv' nicht Verfügbar ist.

Code: Alles auswählen

$ cat date_index_formatter.py
"""
When plotting daily data, a frequent request is to plot the data
ignoring skips, eg no extra spaces for weekends.  This is particularly
common in financial time series, when you may have data for M-F and
not Sat, Sun and you don't want gaps in the x axis.  The approach is
to simply use the integer index for the xdata and a custom tick
Formatter to get the appropriate date string for a given index.
"""

import numpy
from matplotlib.mlab import csv2rec
from pylab import figure, show
import matplotlib.cbook as cbook
from matplotlib.ticker import Formatter

datafile = cbook.get_sample_data('msft.csv', asfileobj=False)
print 'loading', datafile
r = csv2rec(datafile)[-40:]

class MyFormatter(Formatter):
    def __init__(self, dates, fmt='%Y-%m-%d'):
        self.dates = dates
        self.fmt = fmt

    def __call__(self, x, pos=0):
        'Return the label for time x at position pos'
        ind = int(round(x))
        if ind>=len(self.dates) or ind<0: return ''

        return self.dates[ind].strftime(self.fmt)

formatter = MyFormatter(r.date)

fig = figure()
ax = fig.add_subplot(111)
ax.xaxis.set_major_formatter(formatter)
ax.plot(numpy.arange(len(r)), r.close, 'o-')
fig.autofmt_xdate()
show()

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> from matplotlib.mlab import csv2rec
>>> from pylab import figure, show
>>> import matplotlib.cbook as cbook
>>> from matplotlib.ticker import Formatter
Wie könnte man das Problem beheben oder gibt es ein anderes Beispiel wo die Datenpunkte im Graphen markiert sind und dass vielleicht die Eingabewerte in einer Datenstruktur sich befinden?

Viele Dank im Voraus.

Re: matplotlib problem

Verfasst: Mittwoch 18. August 2010, 09:36
von ms4py
Für diese Markierungen ist nur der dritte Parameter der `plot`-Funktion verantwortlich, alles andere ist in deinem Fall unnötiger Overhead...

Code: Alles auswählen

In [1]: from pylab import *

In [2]: x = arange(20)

In [3]: y = sin(x)

In [4]: plot(x, y, 'o-')
Out[4]: [<matplotlib.lines.Line2D object at 0xa3b7f4c>]

In [5]: show()

Re: matplotlib problem

Verfasst: Freitag 20. August 2010, 13:05
von mit
Danke, ich habe den Code einwenig erweitert, aber leider werden an der X und Y-Achse die Wert 0, 5, 10,.. hoch gezählt.

Code: Alles auswählen

from pylab import *
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

y = [20, 24, 8, 4, 12, 22, 31, 25, 15, 28, 12, 27, 22, 22, 27, 14, 32, 28, 8, 17, 2, 8, 29, 13, 14, 20, 11, 28, 8]

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(x, y, 'o-')
ax.grid(True)

# rotates and right aligns the x labels, and moves the bottom of the
# axes up to make room for them
fig.autofmt_xdate()
plt.show()
Wie könnete man die X und Y-Achse Werte ändern damit man 1,2,3,4,... bekommt?

Re: matplotlib problem

Verfasst: Freitag 20. August 2010, 15:09
von mit
Habe das Problem teilweise wie folgt gelöst:

Code: Alles auswählen

from pylab import *
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

y = [20, 24, 8, 4, 12, 22, 31, 25, 15, 28, 12, 27, 22, 22, 27, 14, 32, 28, 8, 17, 2, 8, 29, 13, 14, 20, 11, 28, 8]

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(x, y, 'o-')
ax.grid(True)

# rotates and right aligns the x labels, and moves the bottom of the
# axes up to make room for them
fig.autofmt_xdate()

# set x and y labels
plt.xticks(range(0, 40, 1))
plt.yticks(range(0, 40, 1))
plt.show()
Nun liegen die Werte an den Achsen zu eng beieinander. Gibt es eine Möglichkeit den Abstand zwischen den Werten an den Achsen zu vergrößern?

Re: matplotlib problem

Verfasst: Freitag 20. August 2010, 17:10
von DaMutz
Warum willst du denn die x- und yticks in 1-ner Schritten erhöhen? Es ist doch nunmal so, wenn du 5mal mehr Zahlen willst braucht es auch mehr Platz. Meiner Meinung nach reichen die 5er Werte.

Re: matplotlib problem

Verfasst: Samstag 21. August 2010, 08:37
von mit
Es ist schwer die Koordinaten Werte zu erkennen wenn man nur die 5er Schritte verwendet. Deshalb wollte ich die 1er Schritte haben.

UPDATE:
Wenn ich das plt.show Fenster vergrößere siehen die 1er Abstände gut aus. Gibt es eine Möglichkeit den Graphen zu erlauben größer zu sein als das plt.show Fenster?

Re: matplotlib problem

Verfasst: Samstag 21. August 2010, 09:31
von ms4py
Du könntest die Größe der Figure mit dem Parameter `figsize` festlegen, s. http://matplotlib.sourceforge.net/api/p ... lot.figure

Re: matplotlib problem

Verfasst: Samstag 21. August 2010, 13:00
von mit
Mit fig = plt.figure(figsize=(40,40)) werden die PNG Bilder (plt.savefig('test.png')) einwenig zu groß. Gibt es vielleicht eine Formel um die beste Bildgröße abhängig von den X,Y-Koordinaten zu bestimmen?

Ein anderes Problem ist, dass plt.show() ein großes Fenster für das grosse Bild erzeugt. Gibt eine Möglichkeit damit das Fenster klein bleibt und es nur mit der Mauszeiger möglich das Bild zu veschiben (wie Google Earth) ?