3
$\begingroup$

I have imported my text-type data as a matrix. I want to make a scatter plot (ListPlot) using one column as x-axis and other columns as y-axis.

Is there a function F which can make a scatter plot in the following form?

F[matrix[[All, 1]], {matrix[[All,2],...,matrix[[All,n]]]]
$\endgroup$

5 Answers 5

5
$\begingroup$

E.g.:

test = {{1, 2, 3}, {2, 4, 6}, {3, 5, 9, 11}};

ListPlot[Map[Thread[List[#[[1]], #[[2 ;;]]]] &, test], PlotStyle -> PointSize[.02]]

enter image description here

$\endgroup$
6
  • $\begingroup$ Dear, I really cannot understand your method.Fist, my data is a matrix why still using List to form a list. Sceond, what does the function Thread and Map here. I have searched these functions in reference center. I could understand these functions respectively. But, when these functions combined, it really confused me. $\endgroup$
    – wikichung
    Commented May 19, 2014 at 12:20
  • $\begingroup$ @wikichung: Arrays are just specializations of lists (they are lists, in other words). The Thread piece is a short way of making a list of lists with some constant element, Map takes that over all of the data. Let me know if that's clear... $\endgroup$
    – ciao
    Commented May 20, 2014 at 2:27
  • $\begingroup$ it works and can plot many lines $\endgroup$
    – wikichung
    Commented May 23, 2014 at 0:54
  • $\begingroup$ @wikichung: Glad to hear. Consider accepting an answer (little check-mark) that best fits your question. $\endgroup$
    – ciao
    Commented May 23, 2014 at 1:03
  • $\begingroup$ it works and can plot many lines.ListPlot[Map[Thread[List[#[[1]], {#[[i]],#[[j]]}]] &, test], PlotStyle -> PointSize[.02]] $\endgroup$
    – wikichung
    Commented May 23, 2014 at 1:08
4
$\begingroup$

The new-in-9 TemporalData can be convenient for the current task:

data = RandomReal[10, {30, 5}];
td = TemporalData[Rest@Transpose@data, {First@Transpose@data}];
ListPlot[td, Joined -> True, PlotMarkers -> Automatic, ImageSize -> 400]

enter image description here

$\endgroup$
3
  • $\begingroup$ I tried your method, but it did not work. :( $\endgroup$
    – wikichung
    Commented May 19, 2014 at 12:07
  • $\begingroup$ @wikichung, what are the Dimensions of your matrix (what do you get when you evaluate Dimensions[matrix]?) Does the method work if you remove the Transpose@ pieces in the code in this post? $\endgroup$
    – kglr
    Commented May 19, 2014 at 13:08
  • $\begingroup$ my data are collected by a collector, so it is a 8000x9 matrix. The first column is the time, and others column is my signals. $\endgroup$
    – wikichung
    Commented May 20, 2014 at 1:39
2
$\begingroup$

Try this.

This approach correct if Length[xAxisdData]==Length[yAxesdData[[i]]]

xAxisdData={1,2,3,4};
yAxesdData={{1,2,3,4},{1,4,9,16}};
dataToPlot=Transpose[{xAxisdData,#}]&/@yAxesdData;
ListPlot[dataToPlot,PlotStyle->PointSize[.02]]

enter image description here

$\endgroup$
1
$\begingroup$

Suppose your matrix looks like:

list = Table[5 i + j, {i, 10}, {j, 10}];

if so, you can convert the table into the matrixform with:

MatrixForm[list];

and plot every Line as usual:

ListPlot[{list[[1]], list[[2]], list[[8]]}]

plot1

If you like to plot Line 1 vs Line 6 you can use:

ListPlot[{list[[1]], list[[6]]} // Transpose]

plot2

$\endgroup$
2
  • $\begingroup$ Dear, my data are collected by a collector, so it is a 8000x9 matrix. The first column is the time, and others column is my signals. It seems that my matrix does not fit your supposed form. $\endgroup$
    – wikichung
    Commented May 20, 2014 at 1:38
  • $\begingroup$ My data are collected by a collector, so it is a 8000x9 matrix. The first column is the time, and others column is my signals. it seems that my matrix does not fit your supposed form. $\endgroup$
    – wikichung
    Commented May 20, 2014 at 1:42
1
$\begingroup$

For this, Part (shorthand [[]]) is your friend. Using

data = RandomReal[{0, 10}, {20, 5}];

as the data set, you can extract multiple columns as follows:

data[[All, {x, y}]]

where x is the x-column and y is the y-column. Note, x can equal y and even be larger than y. Their values are only constrained by the number of columns, with one exception: negative indices are treated as starting from the end of the list, not the beginning like positive indices. To get multiple data sets, you have to be a little creative,

data[[All, {1, #}]]& /@ Range[2, Length@data[[1]]]
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.