How to plot data with lines like “<text> <datetime>”, with gnuplot?

柔情痞子 提交于 2020-01-05 11:04:26

问题


My data is a typical CSV-like text where each line consists of two columns where the first is text and the second a unix timestamp, as follows:

Mom 1441008169
Dad 1442516527
Ken 1441783871
...
<text> <unix-timestamp>

I thought I could arrange the data along a timeline, drawing dots/shapes of color corresponding to the text in the line, at a point along the x axis corresponding to the timestamp. At best I got gnuplot to tell me:

line 0: Need using spec for y time data

when I tell it to:

set ydata time
set timefmt "%s"
plot "-"
<data>
EOF

I want to render a plot using dots, or diamonds or shapes with color corresponding to the text string in first column. In other words, if my text values fall within the set {"Mom", "Dad", "Ken"}, then gnuplot should draw these shapes corresponding to "Mom" in red, "Dad" in green, and "Ken" in blue, or something like that, at points corresponding to their respective timestamps along the x axis.

The challenge for me is to have gnuplot distinguish between the text strings. The data can be thought of as, for instance, incoming calls from a person where the timestamp indicates date and time for the call and text represents the person calling. I thought representing these data as plotted dots/orbs/diamonds/whatever of different color along a time line would be a good idea, visually.

How would I achieve that? I can, optionally, generate some sort of identifier table where the unique text strings are each equated to a unique sequential generated ID, if that helps gnuplot.


回答1:


I guess what you want is something like this

The x-axis spans the time interval which is specified by your data file (2nd column). Each name (Ken, Mom, Dad) is represented by a different point type (pt) and a specific colour (lc rgb 'color').

You can generate this plot by the following commands (assuming your data file's name is 'test'):

set xdata time
set timefmt "%s"
set format x "%d/%m/%y"
set xtics rotate
unset ytics
set yrange[0:2]

plot 'test' u ($2):(stringcolumn(1) eq "Ken" ? 1 :1/0) w p pt 5 ps 1.5 lc rgb 'blue' t 'Ken',\
    '' u ($2):(stringcolumn(1) eq "Dad" ? 1 :1/0) w p pt 7 ps 1.5 lc rgb 'red' t 'Dad',\
    '' u ($2):(stringcolumn(1) eq "Mom" ? 1 :1/0) w p pt 3 ps 1.5 lc rgb 'green' t 'Mom'

You can use different point types by assigning different numbers to pt. ps specifies the point size.

Another representation I came up with is the following: You can generate it with:

plot 'test' u ($2):(1):(stringcolumn(1)) with labels t '',\
 '' u ($2):(0.95) with impulses t ''

I hope this answers your question, and it is what you were looking for.



来源:https://stackoverflow.com/questions/33425785/how-to-plot-data-with-lines-like-text-datetime-with-gnuplot

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