mathematica 分段函数使用不同的颜色

环境: mathematica 12.0, fedora 29 workstation x86_64
smooth L1:
f ( x ) = { 0.5 x 2 ∣ x ∣ < 1 ∣ x ∣ − 0.5 otherwise f(x)= \begin{cases} 0.5x^2 & \mid x\mid<1\\ \mid x\mid-0.5 & \text{otherwise} \end{cases} f(x)={0.5x2x0.5x<1otherwise

code snipet 1:

f = Piecewise[{{0.5 #^2, Abs[#] < 1}, {Abs[#] - 0.5,  Abs[#] >= 1}, {Log[#], 2 < #}}] &;

colorFunction = f;
piecewiseParts = Length@colorFunction[[1, 1]];
colors = ColorData[1][#] & /@ Range@piecewiseParts;
colorFunction[[1, 1, All, 1]] = colors;

Plot[f[x], {x, -2, 2}, ColorFunction -> colorFunction, ColorFunctionScaling -> False]

code snipet 2:

pwSplit[_[pairs : {{_, _} ..}]] := 
 Piecewise[{#}, Indeterminate] & /@ pairs

pwSplit[_[pairs : {{_, _} ..}, expr_]] := 
 Append[pwSplit@{pairs}, pwSplit@{{{expr, Nor @@ pairs[[All, 2]]}}}]

pw = Piecewise[{{0.5 x^2, Abs[x] < 1}, {Abs[x] - 0.5, Abs[x] >= 1}}];

Plot[Evaluate[pwSplit@pw], {x, -2, 2}, PlotStyle -> Thick,  Axes -> True]

smoothL1

[1] https://mathematica.stackexchange.com/questions/1128/plotting-piecewise-function-with-distinct-colors-in-each-section

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值