如何根据数据集以直线和不同的线型绘制数据

如何解决如何根据数据集以直线和不同的线型绘制数据

我有一个唯一的数据集(行和列的数量视情况而定。)

0.0       0       0       0       0       0       0
0.5       0       0       0       0       0       0
2.0 156.626 156.626 138.354 138.354 138.354 138.354
2.5 156.626 156.626 138.354 138.354 138.354 138.354
4.0 287.268 287.268 289.808 289.808 271.829 276.304
4.5 287.268 287.268 289.808 289.808 271.829 276.304
6.0 418.931 426.263 418.933 426.259 273.572 273.559
6.5 418.931 426.263 418.933 426.259 273.572 273.559
8.0 417.211 417.21  417.207 417.211 417.207 417.212
8.5 417.211 417.21  417.207 417.211 417.207 417.212

如您所见,它具有数据集的唯一组合(先更改然后再更改,再更改然后再更改。)我想在实线中绘制常量数据集,而不用任何破折号,而将非直线将为不同的破折号。 我需要一个脚本(无论是gnuplot还是matplotlib)都可以按照附图绘制数据。

enter image description here

在此图中,我仅显示了三行,作为示例。

我已经在gnuplot脚本下创建了可以给我所需的图表(

enter image description here

,但它没有给我水平线,没有任何破折号的实线。

CASE = "New.dat"
Xi=-2 ; Xf=22; Xs=1

AYf=500 ; AYs=100
reset
set terminal postscript eps enhanced size 20cm,20cm  color solid lw 3 "Times-Bold" 40
set output "data.eps" 
set multiplot \
    layout 1,1 rowsfirst \
    title "{/:Bold=40 }" \
    margins screen 0.15,0.85,0.11,0.950 \
    spacing screen 0.00,0.03

set key spacing 1.2
set    mxtics 2 
set    mytics 2
unset key
unset arrow
set arrow from Xi,0.00 to Xf,000 nohead lw 3.5  lc rgb "blue" lt 0
set xrange [Xi:Xf]
set yrange [-50:AYf]
set key at graph 0.63,0.95 font "Times-bold,30"
set xtics Xi,Xs,Xf format ""
set ytics 0,AYs,AYf format "%g" font "Times-bold,40"

plot CASE u 1:2 title "Path-1"  w l  lc 1 lw 3 dashtype 2,CASE u 1:3 title "Path-2"  w l lc 2 lw 3 dashtype 3,CASE u 1:4 title "Path-3" w l lc 4 lw 3 dashtype 4,CASE u 1:5 title "Path-4" w l lc 6 lw 3 dashtype 5,CASE u 1:6 title "Path-5" w l  lc 7 lw 3 dashtype 6,CASE u 1:7 title "Path-6" w l lc 9 lw 3 dashtype 9

这是来自theozh的脚本

enter image description here

解决方法

在gnuplot中,我会这样做。 绘制两次数据

  1. with lines和不同的破折号
  2. 和水平线with vectors,但前提是y值不变。

区分虚线有点困难,因为其中一些虚线是相互重叠的。您需要对此进行一些优化。

代码:

### plot intermittent horizontal lines 
reset session

$Data <<EOD
0.0       0       0       0       0       0       0
0.5       0       0       0       0       0       0
2.0 156.626 156.626 138.354 138.354 138.354 138.354
2.5 156.626 156.626 138.354 138.354 138.354 138.354
4.0 287.268 287.268 289.808 289.808 271.829 276.304
4.5 287.268 287.268 289.808 289.808 271.829 276.304
6.0 418.931 426.263 418.933 426.259 273.572 273.559
6.5 418.931 426.263 418.933 426.259 273.572 273.559
8.0 417.211 417.21  417.207 417.211 417.207 417.212
8.5 417.211 417.21  417.207 417.211 417.207 417.212
EOD

set key top left

set datafile missing NaN    # apparently necessary for gnuplot 5.2.2 

plot for [i=2:7] $Data u 1:i w l lw 2 lc i-1 dt i title sprintf("Path %d",i-1),\
     for [i=2:7] y1=x1=NaN $Data u (x0=x1,x1=column(1),x0):(y0=y1,y1=column(i)):(x1-x0):(y0==y1?0:NaN) w vectors lw 4 lc i-1 nohead notitle
### end of code

结果:

enter image description here

,

计算与数据的平稳段和递增段相对应的索引。然后按段绘制。这是第一条路径的图。要绘制所有路径,可以使用另一个for循环。也许有一个更简单的解决方案,但这应该可行。

# path data
x = np.array([0.,0.5,2.,2.5,4.,4.5,6.,6.5,8.,8.5])
y = np.array([0.,0.,156.626,287.268,418.931,417.211,417.211])

# indices of plateau and increasing
idx_plat = np.where(np.diff(y) == 0)[0]
idx_incr = np.where(np.diff(y) != 0)[0]

# color for first path
color = 'C0'

# text setup
texts = ['A','B','C','D','E']
off = 10 # y offset for text

# plot plateau and increasing segments in a loop
for i in range(len(idx_plat) - 1):
    x_sub = x[idx_plat[i]:idx_plat[i+1]]
    y_sub = y[idx_plat[i]:idx_plat[i+1]]
    
    plt.plot(x_sub,y_sub,linestyle = '--',color = color)
    
    # annotate text
    plt.text(x_sub.mean(),y_sub[0] + off,texts[i])
    
for j in range(len(idx_incr) - 1):
    x_sub = x[idx_incr[j]:idx_incr[j+1]]
    y_sub = y[idx_incr[j]:idx_incr[j+1]]
    
    plt.plot(x_sub,linestyle = '-',color = color)

# plot last segment twice with label to create legend
plt.plot(x_sub,color = color,label = label)
plt.legend(loc = 'best')

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res