hauchun10
Posts: 8
Joined: Mon Apr 03, 2017 7:10 am

Using a csv file to ploy a x-y scatter graph

Mon Apr 03, 2017 7:25 am

Hello everyone,
I am a beginner of Pi and programming and I met a difficulty in "converting" a csv file to a x-y scatter programmatically.
My wanted output is as follows:
Starting from column J, I would like to use the entire row 12 data as x-axis value and row 13 data as y-axis value.

I am considering if transpose need to be applied. However, some example shown on the internet would say transposition is not necessary. Could anyone solve my problem?
Many Thanks

MarkyV
Posts: 97
Joined: Wed Sep 21, 2016 1:52 pm
Location: Buckingham, UK
Contact: Website

Re: Using a csv file to ploy a x-y scatter graph

Mon Apr 03, 2017 12:38 pm

Hi,

What programming system are you using (python, scratch, node-red, c)? and how are you loading the csv file?

If you are using something like python it will be a fairly simple case of reading through the data and outputting it.

Mark
My RaspberryPi Blog: http://thepimaker.online

hauchun10
Posts: 8
Joined: Mon Apr 03, 2017 7:10 am

Re: Using a csv file to ploy a x-y scatter graph

Mon Apr 03, 2017 1:07 pm

Hi Mark,
I would like to use python as it pre installed in Pi.
However, with some unwanted information, it seems more difficult to program.
Moreover, I nearly know nothing in programming.
Thanks

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

Re: Using a csv file to ploy a x-y scatter graph

Mon Apr 03, 2017 1:15 pm

Which of the following best describes your situation (note that these are not mutually exclusive):

1) I want to learn programming in general

2) I want to learn Python specifically

3) I just want to plot my data (I.e., this is a one off or it is part of a general research project not related to the Pi or Python).

If it is #3, then you can't do better than to install and learn 'gnuplot'.
It is quite easy to get up and running with gnuplot, following one of the many online tutorials.
If this post appears in the wrong forums category, my apologies.

hauchun10
Posts: 8
Joined: Mon Apr 03, 2017 7:10 am

Re: Using a csv file to ploy a x-y scatter graph

Mon Apr 03, 2017 2:07 pm

Martin Frezman wrote:Which of the following best describes your situation (note that these are not mutually exclusive):

1) I want to learn programming in general

2) I want to learn Python specifically

3) I just want to plot my data (I.e., this is a one off or it is part of a general research project not related to the Pi or Python).

If it is #3, then you can't do better than to install and learn 'gnuplot'.
It is quite easy to get up and running with gnuplot, following one of the many online tutorials.
Hello Martin,
My situation is 3 exactly.
I know gnuplot is a powerful tool for plotting graph. However, I cannot find similar example to my case. Moreover, I am rushed for a project recently and I am afraid I cannot complete in time if I just learn from tutorial by myself. Therefore, I would like somebody to share the knowledge to me.
Thanks.

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

Re: Using a csv file to ploy a x-y scatter graph

Mon Apr 03, 2017 2:44 pm

I know you say that time is of the essence, and it may be difficult to get this going fast enough using the Internet as our communication path, but if you would post your data file and a short description of what you want and why you think gnuplot can't do it, I'm pretty sure I could get it done with gnuplot.
If this post appears in the wrong forums category, my apologies.

hauchun10
Posts: 8
Joined: Mon Apr 03, 2017 7:10 am

Re: Using a csv file to ploy a x-y scatter graph

Mon Apr 03, 2017 3:42 pm

Martin Frezman wrote:I know you say that time is of the essence, and it may be difficult to get this going fast enough using the Internet as our communication path, but if you would post your data file and a short description of what you want and why you think gnuplot can't do it, I'm pretty sure I could get it done with gnuplot.
Hi Martin. I am not saying gnuplot can't do it. Instead, I am not familiar to gnuplot.
My csv file is as follows:
trigger stamp channel index type delay factor rate count data
8463 17:30 0 4733 0 0 1 1.14 5698 0.6828 0.79063 0.89844, ... (5695 remaining)
8464 17:31 0 4733 0 0 1 1.14 5698 -0.0043 -0.0085 -0.0043, ... (5695 remaining)
8465 17:32 0 4733 0 0 1 1.14 5698 0.6109 -0.0043 -0.0085, ... (5695 remaining)
8466 17:33 0 4733 0 0 1 1.14 5698 -0.0043 0.7906 0.8984, ...(5695 remaining)
... ..... .... ............ .... ... .... ..... ..... ....... .......... ................
My objective is, for example, to use row 8 as x-axis value and use row 9 as y-axis value, while the desired data are selected starting from column J (under data) to the end of the row.
Thanks

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

Re: Using a csv file to ploy a x-y scatter graph

Mon Apr 03, 2017 4:43 pm

My objective is, for example, to use row 8 as x-axis value and use row 9 as y-axis value, while the desired data are selected starting from column J (under data) to the end of the row.
I'm totally confused by this. Maybe someone else can help me out.

Are you sure you're not using the word 'row' when you really mean 'column' (when you say 'row 8' and 'row 9') ?

And what is 'column J' ?

And what is 'under data' ?
If this post appears in the wrong forums category, my apologies.

hauchun10
Posts: 8
Joined: Mon Apr 03, 2017 7:10 am

Re: Using a csv file to ploy a x-y scatter graph

Mon Apr 03, 2017 5:19 pm

The attachment csv.png is no longer available
Martin Frezman wrote:
My objective is, for example, to use row 8 as x-axis value and use row 9 as y-axis value, while the desired data are selected starting from column J (under data) to the end of the row.
I'm totally confused by this. Maybe someone else can help me out.

Are you sure you're not using the word 'row' when you really mean 'column' (when you say 'row 8' and 'row 9') ?

And what is 'column J' ?

And what is 'under data' ?
I try to explain it.

The attachment shows a part of a csv file opened in MS Excel, therefore column J would exist.
What I want to do is to use row 4 as x-axis value and row 5 as y-axis value, while the selected numbers are starting from column J to the end of the row. Before plotting, I would also like row 5 value is divided by 100 before plotting graph.
csv.png
csv.png (14.57 KiB) Viewed 1203 times

hauchun10
Posts: 8
Joined: Mon Apr 03, 2017 7:10 am

Re: Using a csv file to ploy a x-y scatter graph

Tue Apr 04, 2017 7:58 am

I found a method using matplotlib which may be good at plotting horizontal (row) data, and the code is shown below.

from matplotlib import pyplot, dates
from csv import reader
from dateutil import parser

with open('../data/astro_pi_data_20150824_085954.csv', 'r') as f:
data = list(reader(f))

temp = [i[3] for i in data[1::]]
time = [parser.parse(i[19]) for i in data[1::]]

pyplot.plot(time, temp)
pyplot.show()

However, this code plots a temp vs time curve. Also, how to cope with the surplus information before column "data", and how to divided the row's value by 100 before plotting is still a problem.

Return to “Beginners”