首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gnuplot:散点图和密度

Gnuplot:散点图和密度
EN

Stack Overflow用户
提问于 2017-02-21 21:20:07
回答 3查看 8.5K关注 0票数 5

我有代表星团的x和y数据点。我想使用Gnuplot及其带有重叠点的散射函数来可视化密度。

我使用了以下命令:

代码语言:javascript
复制
 set style fill transparent solid 0.04 noborder
 set style circle radius 0.01
 plot "data.dat" u 1:2 with circles lc rgb "red"

结果是:

然而,我想要那样的东西。

在Gnuplot中这是可能的吗?有什么想法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-12-13 04:54:35

也许,比我之前的答案更好的方法是:在我用了8年的电脑上,1000分大概需要2-3分钟。你基本上可以对3D做同样的事情(参见https://stackoverflow.com/a/53744193/7295599)

代码:

代码语言:javascript
复制
### density color plot 2D
reset session

N = 1000     # number of datapoints
Delta = 0.5    # half boxwidth

TimeStart = time(0.0)
# create some dummy datablock with some distribution
set table $Data
    set samples N
    plot '+' u (invnorm(rand(0))):(invnorm(rand(0))) w table
unset table
print sprintf("Data generated: %.3f sec",time(0.0)-TimeStart)
# end creating dummy data

TimeStart = time(0.0)
# put the datafile/dataset into arrays
stats $Data nooutput
RowCount = STATS_records
array ColX[RowCount]
array ColY[RowCount]
array ColC[RowCount]
do for [i=1:RowCount] {
set table $Dummy
    plot $Data u (ColX[$0+1]=$1,0):(ColY[$0+1]=$2,0) with table
unset table
}
print sprintf("Data put into arrays: %.3f sec",time(0.0)-TimeStart)


# look at each datapoint and its sourrounding
do for [i=1:RowCount] {
    # print sprintf("Datapoint %g of %g",i,RowCount)
    x0 = ColX[i]
    y0 = ColY[i]
    # count the datapoints with distances <Delta around the datapoint of interest
    set table $Occurrences
        plot $Data u ((abs(x0-$1)<Delta) & (abs(y0-$2)<Delta) ? 1 : 0):(1) smooth frequency
    unset table
    # extract the number from $Occurrences which will be used to color the datapoint
    set table $Dummmy
        plot $Occurrences u (c0=$2,0):($0) every ::1::1 with table
    unset table
    ColC[i] = c0
}

# put the arrays into a dataset again
set print $Data
do for [i=1:RowCount] {
    print sprintf("%g\t%g\t%g",ColX[i],ColY[i],ColC[i])
}
set print
print sprintf("Duration: %.3f sec",time(0.0)-TimeStart)

set palette rgb 33,13,10
plot $Data u 1:2:3 w p ps 1 pt 7 lc palette z notitle
### end of code

将导致类似如下的结果:

票数 4
EN

Stack Overflow用户

发布于 2017-02-25 14:57:23

是否可以选择使用imagemagick对图像进行后处理?

代码语言:javascript
复制
# convert into a gray scale image
convert source.png -colorspace gray -sigmoidal-contrast 10,50% gray.png

# build the gradient, the heights have to sum up to 256
convert -size 10x1  gradient:white-white white.png
convert -size 10x85 gradient:red-yellow \
                    gradient:yellow-lightgreen \
                    gradient:lightgreen-blue \
        -append gradient.png
convert gradient.png white.png -append full-gradient.png

# finally convert the picture
convert gray.png full-gradient.png -clut target.png

我没有尝试过,但我非常确定gnuplot可以直接绘制灰度图像。

这是(旋转的)渐变图像:

这就是结果:

票数 1
EN

Stack Overflow用户

发布于 2018-12-12 00:48:47

尽管这个问题相当“老”,而且这个问题可能会以不同的方式解决……这可能更多的是出于好奇心和乐趣,而不是出于实际目的。下面的代码仅使用gnuplot实现了根据点的密度进行着色。在我的旧电脑上,绘制1000个点需要几分钟时间。如果这段代码可以改进,我会很感兴趣,特别是在速度方面(不使用外部工具)。遗憾的是,gnuplot没有提供排序、查找表、合并、转置或其他基本功能(我知道...是gnuPLOT..。而不是分析工具)。

代码:

代码语言:javascript
复制
### density color plot 2D
reset session

# create some dummy datablock with some distribution
N = 1000
set table $Data
    set samples N
    plot '+' u (invnorm(rand(0))):(invnorm(rand(0))) w table
unset table
# end creating dummy data

stats $Data u 1:2 nooutput
XMin = STATS_min_x
XMax = STATS_max_x
YMin = STATS_min_y
YMax = STATS_max_y
XRange = XMax-XMin
YRange = YMax-YMin
XBinCount = 20
YBinCount = 20
BinNo(x,y) = floor((y-YMin)/YRange*YBinCount)*XBinCount + floor((x-XMin)/XRange*XBinCount)

# do the binning
set table $Bins
    plot $Data u (BinNo($1,$2)):(1) smooth freq # with table
unset table

# prepare final data: BinNo, Sum, XPos, YPos
set print $FinalData
do for [i=0:N-1] {
    set table $Data3
    plot $Data u (BinNumber = BinNo($1,$2),$1):(XPos = $1,$1):(YPos = $2,$2) every ::i::i with table
    plot [BinNumber:BinNumber+0.1] $Bins u (BinNumber == $1 ? (PointsInBin = $2,$2) : NaN) with table
    print sprintf("%g\t%g\t%g\t%g", XPos, YPos, BinNumber, PointsInBin)
    unset table
}
set print

# plot data
set multiplot layout 2,1
set rmargin at screen 0.85
plot $Data u 1:2 w p pt 7 lc rgb "#BBFF0000" t "Data"
set xrange restore # use same xrange as previous plot
set yrange restore
set palette rgbformulae 33,13,10
set colorbox
# draw the bin borders
do for [i=0:XBinCount] {
    XBinPos = i/real(XBinCount)*XRange+XMin
    set arrow from  XBinPos,YMin to XBinPos,YMax nohead lc rgb "grey" dt 1
}
do for [i=0:YBinCount] {
    YBinPos = i/real(YBinCount)*YRange+YMin
    set arrow from  XMin,YBinPos to XMax,YBinPos nohead lc rgb "grey" dt 1
}

plot $FinalData u 1:2:4 w p pt 7 ps 0.5 lc palette z t "Density plot"
unset multiplot
### end of code

结果是:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42368448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档