关于等高线图中等高线标签样式的问题
mathematica吧
全部回复
仅看楼主
level 3
zjnone 楼主
怎么在绘制等高线图的时候让等高线在标签处断开,像下图这样
ContourPlot[Cos[\[Pi]/4 (x + 1)] Cos[\[Pi] y], {x, -1, 1}, {y, -1, 1},
ColorFunction -> (Transparent &),
ContourLabels -> (Text[
#3, {#
1, #2}, Background -> White] &)
]
但是不会像下面这样盖住绘制的颜色?
ContourPlot[Cos[\[Pi]/4 (x + 1)] Cos[\[Pi] y], {x, -1, 1}, {y, -1, 1},
ColorFunction -> "SunsetColors",
ContourLabels -> (Text[
#3, {#
1, #2}, Background -> White] &)
]
2021年03月30日 10点03分 1
吧务
level 10
label是通过图元实现的,而label所在位置会有不同背景色的两个区域和一条界限。很难在graphics层面解决此问题,在image层面处理此问题可能容易些。
2021年03月30日 14点03分 2
在graphics层面处理此问题的难度与自己写一个contourplot的难度是同一水平的。
2021年03月30日 14点03分
@asdasd1dsadsa 有道理,我想如果内置功能不支持的话那大概还是不考虑这样绘制等高线图了
2021年03月30日 14点03分
想了想,应该难度差不多。从graphicscomplex里面找到位置接近text的点,然后把对应的Line图元在那附近断开。不过我懒得写了
2021年03月30日 16点03分
@asdasd1dsadsa 你这搞法够呛,不但要断线,还要挖点,万一网格很稀疏(当然这对ContourPlot来说应该不至于)还得加点。
2021年04月03日 03点04分
level 7
也许在图2的基础上用PS处理一下会更好?
也就是从像素的角度考虑,比如一个简陋的示意:
ContourPlot[Cos[\[Pi]/4 (x+1)] Cos[\[Pi] y],{x,-1,1},{y,-1,1},ColorFunction->(Transparent&),ContourLabels->(Text[
#3,{#
1,#2},Background->White]&)]//Rasterize;
ContourPlot[Cos[\[Pi]/4 (x+1)] Cos[\[Pi] y],{x,-1,1},{y,-1,1},ColorFunction->"SunsetColors",ContourStyle->None]//Rasterize;
ImageMultiply[%%,%]
2021年03月31日 02点03分 3
我还从来没有研究过mma的位图处理,这个看起来效果还挺好的
2021年03月31日 02点03分
这个方法确实漂亮。
2021年04月03日 04点04分
吧务
level 15
讲道理我觉得这种画法只适合无色等高线图。不过好不好看终究是主观的东西,这里就不多议论了。
纯在Graphics层面解决不算很难。思路很简单:提取标签的坐标,然后基于这些坐标,返回去把多余的等高线在生成阶段挖掉,最后三幅图合在一起:
wholereg = Rectangle[{-1.1, -1.1}, {1.1, 1.1}];
expr = Cos[π/4 (x + 1)] Cos[π y];
cp = ContourPlot[expr, {x, y} ∈ wholereg, ColorFunction -> (Transparent &),
ContourLabels -> (Text[
#3, {#
1, #2}, Background -> White] &), PlotPoints -> 100]
text = Cases[cp, Text[c_, coord__, _] :> Text[c, coord], Infinity];
coord = text[[All, 2]];
reg = RegionDifference[wholereg, RegionUnion @@ (Ellipsoid[#, {0.05, 0.04}] & /@ coord)];
(* 实践发现先把区域离散再画图更快,至少版本12.2是如此 *)
disreg = DiscretizeRegion[reg, MaxCellMeasure -> 0.005]
cpbroken = ContourPlot[expr, {x, y} ∈ disreg, ColorFunction -> (Transparent &),
PlotPoints -> 100]
cpbg = ContourPlot[expr, {x, y} ∈ wholereg, ColorFunction -> "SunsetColors",
ContourStyle -> None, PlotPoints -> 50];
Show[cpbg, cpbroken, Graphics@text]
2021年04月03日 03点04分 4
说到底,PS最好使
2021年04月03日 03点04分
@asdasd1dsadsa 如果需要高度的自定义(比如,contour label的位置调整),或许如此吧。
2021年04月03日 03点04分
1