How to specify x-axis in List when using Mathematicia and ListPlot?

Deadly 提交于 2019-12-10 23:46:37

问题


I have imported a csv file into a list with this stmt:

data1 = Take[Import["D:\\_reports\\optim_5_60_b_2.csv", "CSV"],   5]

Which gives:

{{178, 8, 9, 1}, {152, 2, 8, 1}, {378, 8, 9, 2},
 {343, 3, 7.5`, 2}, {143, 3, 7.5`, 1}}

I would like to create a plot where the x-axis is based on the first field:

178,152,378,343,143,373,743,352

And the plot creates a line for each subsequent field, so the 2nd field is:

8,2,8,3,3,3,3,2

This would be the first line on the y-axis, the other y-axis values would be plotted in the same manner. I would like the first y-plot to be plotted in red, and the second in blue, and the third in green.


回答1:


And an alternative version:

data = {{178, 8, 9, 1}, {152, 2, 8, 1}, {378, 8, 9, 2},
   {343, 3, 7.5, 2}, {143, 3, 7.5, 1}};

x = data[[All, 1]];

ListLinePlot[
 Transpose[
  Sort[MapThread[Function[xpt, {xpt, #} & /@ #2][#1] &,
    {x, Rest /@ data}]]], PlotStyle -> {Red, Blue, Green},
 PlotMarkers -> {Automatic, 10}, AxesOrigin -> {Min@x, Automatic}]

Similarly, but easier to read:

ListLinePlot[
 Transpose[
  Sort[Table[Map[{data[[i, 1]], #} &, Rest[data[[i]]]],
    {i, Length[data]}]]], PlotStyle -> {Red, Blue, Green},
 PlotMarkers -> {Automatic, 10}, AxesOrigin -> {Min@x, Automatic}]



回答2:


You can do :

data1 = {{178, 8, 9, 1}, {152, 2, 8, 1}, {378, 8, 9, 2}, {343, 3, 7.5,
2}, {143, 3, 7.5, 1}};

tobeplotted =  With[{LocalX = data1[[All, 1]]}, 
   Transpose[{LocalX, #}] & /@ Transpose[data1[[All, 2 ;;]]]]

ListPlot[tobeplotted, PlotStyle -> {Red, Blue, Green},  PlotMarkers -> {Automatic, 10}]




回答3:


I propose this:

dat = {{178, 8, 9, 1}, {152, 2, 8, 1}, {378, 8, 9, 2},
       {343, 3, 7.5`, 2}, {143, 3, 7.5`, 1}};

ListLinePlot[
 Thread[Thread@{#, {##2}} & @@@ Sort@dat],
 PlotStyle -> {Red, Blue, Green}
]



来源:https://stackoverflow.com/questions/12144428/how-to-specify-x-axis-in-list-when-using-mathematicia-and-listplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!