gnuplot - 如何在 gnuplot 5.4 中绘制月份数字?

标签 gnuplot

我正在尝试在 gnuplot 5.4 中获取日期的月份。

考虑以下数据:

2019/01/01
2019/02/01
2019/03/01
2019/04/01
2019/05/01
2019/06/01
2019/07/01
2019/08/01
2019/09/01
2019/10/01
2019/11/01
2019/12/01
2020/01/01
2020/02/01
2020/03/01
2020/04/01
2020/05/01
2020/06/01
2020/07/01
2020/08/01
2020/09/01
2020/10/01
2020/11/01
2020/12/01

对于每个数据点,我想在 x 轴上显示完整日期,在 y 轴上显示月份数字 (0-11)。

gnuplot 文档建议使用 tm_mon function对于这样的任务,它应该返回给定日期的月份数字。

据我了解,以下 gnuplot 脚本应该执行我想要的操作:

#!/bin/gnuplot
set timefmt '%Y/%m/%d'
set xdata time
set format x '%m/%y'
set datafile separator ','

plot "data.csv" using 1:(tm_mon($1)) title 'data'

但事实并非如此。此 gnuplot 脚本在 x 轴上正确显示日期,但在 y 轴上具有常量 0

我做错了什么?为什么tm_mon($1)不断返回0?

最佳答案

我不确定我是否完全明白你的意图。 我知道您希望将第 1 列作为 xtic 标签。 但是,仅将第 1 列作为 xtic 标签或将第 1 列解释为日期/时间并相应地缩放 x 轴之间存在差异,gnuplot 会自动处理 xtic 标签。 对于第一种情况,图表的宽度可能不够宽,无法显示所有标签而不重叠,因此,我重新格式化了日期。检查 help strptimehelp strftime。顺便说一下,help tm_mon 说它需要以秒为单位的输入,而不是像 $1 中那样采用日期格式,可以使用 strptime().

我知道您想要一年中月份的范围从 1 到 12。 但是您要绘制的数据在哪里? 也许以下示例是更好地找出您真正想要什么的起点。

代码:

### plotting dates...
reset session

$Data <<EOD
2019/01/01
2019/02/01
2019/03/01
2019/04/01
2019/05/01
2019/06/01
2019/07/01
2019/08/01
2019/09/01
2019/10/01
2019/11/01
2019/12/01
2020/01/01
2020/02/01
2020/03/01
2020/04/01
2020/05/01
2020/06/01
2020/07/01
2020/08/01
2020/09/01
2020/10/01
2020/11/01
2020/12/01
EOD

myTimeInputFmt = "%Y/%m/%d"
myTimeOutputXFmt = "%Y\n%m\n%d"
set xlabel "Date"
set format x myTimeOutputXFmt   # to get enough space for the 3 lines of the label

set ylabel "Month"
set yrange [0.5:12.5]
set ytics 1

plot $Data u 0:(tm_mon(strptime(myTimeInputFmt,strcol(1)))+1): \
             xtic(strftime(myTimeOutputXFmt,strptime(myTimeInputFmt,strcol(1)))) \
             with points pt 7 lc "red" title 'data'
### end of code

结果:

enter image description here

关于gnuplot - 如何在 gnuplot 5.4 中绘制月份数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65086109/

相关文章:

graph - 如何在 gnuplot 上绘制树/图/网络数据?

gnuplot - gnuplot 标签中的逗号

gnuplot - 在 gnuplot 中设置轴刻度标签

gnuplot - Gnuplot和复指数

macos - 在OSX上安装gnuplot 5.0.1

gnuplot:绘制来自标准输入的两个数据集

gnuplot ,非数字重复 x 值

gnuplot - gnuplot直方图: How to put values on top of bars

c++ - 找不到用 C++ 编写的 gnuplot.exe 路径

arrays - 在 gnuplot 中循环遍历数组