0

I've created a script that downloads information about specific currency over a period of month and then stores it as .DAT file. An exemplary .DAT file looks like this:

2015-11-19 4.2477

2015-11-18 4.2509

2015-11-17 4.2433

2015-11-16 4.2472

2015-11-13 4.2362

2015-11-12 4.2245

2015-11-10 4.2485

and so on... Thus, you see that all is stored in form of two columns. Now, the problem: I want to make a graph out of it. I tried to used PyX for it but somehow it cannot put dates on x axis. Do you know how can I plot a graph out of these two columns (that .DAT file)? Thanks in advance!

aleksy.t
  • 267
  • 2
  • 18
  • Would you be able to change the date format to YYYY.MMDD? If so, the graphing module could probably read it and it would still almost be in the format you have right now. – Hans VK Nov 24 '15 at 20:58
  • g.plot(graph.data.file("exemplary.dat", xname=1, y=2), [graph.style.bar()]) for pyX – A.H Nov 24 '15 at 22:21

2 Answers2

0

I would use pandas for this and matplotlib.

To make it easier you will need to insert a header line into your file prior to reading into data frame.

Data

    Date xRate
    2015-11-19 4.2477
    2015-11-18 4.2509
    2015-11-17 4.2433
    2015-11-16 4.2472
    2015-11-13 4.2362
    2015-11-12 4.2245
    2015-11-10 4.2485

Code

    import pandas as pd 
    import numpy as np
    import matplotlib as plt
    # Put the data in the clipboard
    clipdf = pd.read_clipboard()

    df = clipdf.set_index('Date')
    plt.figure(); df.plot(); plt.legend(loc='best')
    plt.show()

enter image description here

ctrl-alt-delete
  • 3,696
  • 2
  • 24
  • 37
  • I tried to install pandas, numpy and matplotlib through Anaconda 'cause I didn't have those. However, even though I installed Anaconda program and did all 'conda install/update matplotlib' stuff and even updated conda and anaconda through both PowerShell and Anaconda Prompt, script doesn't work and I don't know why. – aleksy.t Nov 25 '15 at 02:22
  • When I do 'conda list' it displays me matplot, numpy, pandas and all other packages there, yet script won't work. What I get is this: File "graph_z_pliku.py", line 1, in import pandas as pd File "C:\Anaconda2\lib\site-packages\pandas\__init__.py", line 13, in "extensions first.".format(module)) ImportError: C extension: No module named ma not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace' to build the C extensions first. – aleksy.t Nov 25 '15 at 02:23
  • My advice would be to back up any personal work then uninstall Anaconda using their uninstallers. Then check your C: drive and remove any other folders related to Anaconda. Then download the latest Anaconda. I installed both 3.5 and 2.7 on my desktop. **unless you specifically need an upto date package I would only run conda update conda initially** Check also that you are calling the correct Python environment. Conda allows you to create virtual environments. A typical use case is so that you can test existing code with new packages. Or create separate envs for different projects. – ctrl-alt-delete Nov 25 '15 at 07:56
0

There is no official time axis support in PyX yet. However, you can get it working using the following code. Unfortunately you have to set ticks and a texter yourself.

import time, datetime
from pyx import *
from pyx.graph.axis import timeaxis
from pyx.graph import data

# read the data with the first column being strings
d = data.file("timeaxis.dat", date=1, value=2)

# recreate the data while converting the date column to datetime
d = data.points([[datetime.datetime(*map(int, date.split('-'))), value]
                  for date, value in zip(d.columns["date"], d.columns["value"])], x=1, y=2)

g = graph.graphxy(height=5, x=timeaxis.timeaxis(manualticks=[timeaxis.timetick(2015, 11, 10),
                                                             timeaxis.timetick(2015, 11, 12),
                                                             timeaxis.timetick(2015, 11, 14),
                                                             timeaxis.timetick(2015, 11, 16),
                                                             timeaxis.timetick(2015, 11, 18),
                                                             timeaxis.timetick(2015, 11, 20)],
                                                texter=timeaxis.timetexter("%b %d")))
g.plot(d)
g.writePDFfile()

This will result in the following output:

output of the script

wobsta
  • 721
  • 4
  • 5