<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Mathematica 问答社区 - 分类 'SE' 的近期问答</title>
<link>https://mmaqa.com/qa/qa/se</link>
<description>Powered by Question2Answer</description>
<item>
<title>已回答：【SE】如何画出xkcd样式的图？</title>
<link>https://mmaqa.com/qa/1870/sexkcd?show=1871#a1871</link>
<description>

&lt;p&gt;本问题由以下网友翻译整理：&lt;strong&gt;雾霭&lt;/strong&gt;，&lt;strong&gt;瓦屋青衣&lt;/strong&gt;&lt;/p&gt;



&lt;hr&gt;


&lt;p&gt;通过下面的代码，我们可以用MMA画出XKCD样式的各式图表。实现的方法是通过首先改变MMA原图中的图形（例如线，字体等），然后对整个图像做变形。&lt;/p&gt;



&lt;div style=&quot;background:#eee; border:1px solid #ccc; padding:5px 10px&quot;&gt;The code below attempts to apply the XKCD style to a variety of plots and charts. The idea is to first apply cartoon-like styles to the graphics objects (thick lines, silly font etc), and then to apply a distortion using image processing.&lt;/div&gt;



&lt;p&gt;通过使用最终的程序&lt;code&gt;xkcdConvert&amp;nbsp;&lt;/code&gt;，我们可以很简单的画出xkcd式的基本图表。通过改变&lt;code&gt;xkcdStyle&lt;/code&gt;&amp;nbsp;中的参数，我们可以改变输出的字体和大小。因为对整体图像做变形的时候同样会变形其中的字体，而使用了Comic Sans字体。本程序中没有使用Humor Sans的原因是为了防止出现不好辨认的情况。&lt;code&gt;xkcdLabel&lt;/code&gt;&amp;nbsp;的功能是用来在图中方便的添加标签。&lt;code&gt;xkcdLabel[{str,{x1,y1},{xo,yo}}&lt;/code&gt;各参数为：str代表要添加的标注，&lt;code&gt;{x1,y1}&lt;/code&gt;&amp;nbsp;为添加的标注线的位置，&lt;code&gt;{xo,yo}&lt;/code&gt;&amp;nbsp;为标注的位置。下面是几个基本的例子：&lt;/p&gt;



&lt;div style=&quot;background:#eee; border:1px solid #ccc; padding:5px 10px&quot;&gt;The final function is&amp;nbsp;&lt;code&gt;xkcdConvert&lt;/code&gt;&amp;nbsp;which is simply applied to a standard plot or chart.&lt;/div&gt;



&lt;div style=&quot;background:#eee; border:1px solid #ccc; padding:5px 10px&quot;&gt;The font style and size are set by&amp;nbsp;&lt;code&gt;xkcdStyle&lt;/code&gt;&amp;nbsp;which can be changed to your preference. I&amp;#39;ve used the dreaded Comic Sans font, as the text will get distorted along with everything else and I thought that starting with the Humor Sans font might lead to unreadable text.&lt;/div&gt;



&lt;div style=&quot;background:#eee; border:1px solid #ccc; padding:5px 10px&quot;&gt;The function&amp;nbsp;&lt;code&gt;xkcdLabel&lt;/code&gt;&amp;nbsp;is provided to allow labelling of plot lines using a little callout. The usage is&amp;nbsp;&lt;code&gt;xkcdLabel[{str,{x1,y1},{xo,yo}]&lt;/code&gt;&amp;nbsp;where&amp;nbsp;&lt;code&gt;str&lt;/code&gt;&amp;nbsp;is the label (e.g. a string),&amp;nbsp;&lt;code&gt;{x1,y1}&lt;/code&gt;&amp;nbsp;is the position of the callout line and&amp;nbsp;&lt;code&gt;{xo,yo}&lt;/code&gt;&amp;nbsp;is the offset determining the relative position of the label. The first example demonstrates its usage.&lt;/div&gt;



&lt;pre class=&quot;prettyprint lang-mma&quot;&gt;
xkcdStyle = {FontFamily -&amp;gt; &amp;quot;Comic Sans MS&amp;quot;, 16};

xkcdLabel[{str_, {x1_, y1_}, {xo_, yo_}}] := Module[{x2, y2},
   x2 = x1 + xo; y2 = y1 + yo;
   {Inset[
     Style[str, xkcdStyle], {x2, y2}, {1.2 Sign[x1 - x2], 
      Sign[y1 - y2] Boole[x1 == x2]}], Thick, 
    BezierCurve[{{0.9 x1 + 0.1 x2, 0.9 y1 + 0.1 y2}, {x1, y2}, {x2, y2}}]}];

xkcdRules = {EdgeForm[ef:Except[None]] :&amp;gt; EdgeForm[Flatten@{ef, Thick, Black}], 
   Style[x_, st_] :&amp;gt; Style[x, xkcdStyle], 
   Pane[s_String] :&amp;gt; Pane[Style[s, xkcdStyle]],
   {h_Hue, l_Line} :&amp;gt; {Thickness[0.02], White, l, Thick, h, l},
   Grid[{{g_Graphics, s_String}}] :&amp;gt; Grid[{{g, Style[s, xkcdStyle]}}],
   Rule[PlotLabel, lab_] :&amp;gt; Rule[PlotLabel, Style[lab, xkcdStyle]]};

xkcdShow[p_] := Show[p, AxesStyle -&amp;gt; Thick, LabelStyle -&amp;gt; xkcdStyle] /. xkcdRules

xkcdShow[Labeled[p_, rest__]] := 
 Labeled[Show[p, AxesStyle -&amp;gt; Thick, LabelStyle -&amp;gt; xkcdStyle], rest] /. xkcdRules

xkcdDistort[p_] := Module[{r, ix, iy},
   r = ImagePad[Rasterize@p, 10, Padding -&amp;gt; White];
   {ix, iy} = 
    Table[RandomImage[{-1, 1}, ImageDimensions@r]~ImageConvolve~
      GaussianMatrix[10], {2}];
   ImagePad[ImageTransformation[r, 
     # + 15 {ImageValue[ix, #], ImageValue[iy, #]} &amp;amp;, DataRange -&amp;gt; Full], -5]];

xkcdConvert[x_] := xkcdDistort[xkcdShow[x]]
&lt;/pre&gt;



&lt;hr&gt;


&lt;p&gt;&lt;strong&gt;一些示例：&lt;/strong&gt;&lt;/p&gt;



&lt;pre class=&quot;prettyprint lang-mma&quot;&gt;
f1[x_] := 5 + 50 (1 + Erf[x - 5]);
f2[x_] := 20 + 30 (1 - Erf[x - 5]);
xkcdConvert[Plot[{f1[x], f2[x]}, {x, 0, 10},
  Epilog -&amp;gt; 
   xkcdLabel /@ {{&amp;quot;Label 1&amp;quot;, {1, f1[1]}, {1, 30}}, {&amp;quot;Label 2&amp;quot;, {8, f2[8]}, {0, 30}}},
  Ticks -&amp;gt; {{{3.5, &amp;quot;1st Event&amp;quot;}, {7, &amp;quot;2nd Event&amp;quot;}}, Automatic}]]&lt;/pre&gt;



&lt;p&gt;&lt;img alt=&quot;&quot; src=&quot;https://mmaqa.com/qa/?qa=blob&amp;amp;qa_blobid=16947843295765999248&quot; style=&quot;height:241px; width:370px&quot;&gt;&lt;/p&gt;



&lt;hr&gt;


&lt;pre class=&quot;prettyprint lang-mma&quot;&gt;
xkcdConvert[BarChart[{10, 1}, ChartLabels -&amp;gt; {&amp;quot;XKCD&amp;quot;, &amp;quot;Others&amp;quot;},
  PlotLabel -&amp;gt; &amp;quot;Popularity of questions on MMA.SE&amp;quot;,
  Ticks -&amp;gt; {None, {{1, &amp;quot;Min&amp;quot;}, {10, &amp;quot;Max&amp;quot;}}}]]&lt;/pre&gt;



&lt;p&gt;&lt;img alt=&quot;&quot; src=&quot;https://mmaqa.com/qa/?qa=blob&amp;amp;qa_blobid=2236163457464280040&quot; style=&quot;height:268px; width:370px&quot;&gt;&lt;/p&gt;



&lt;hr&gt;


&lt;pre class=&quot;prettyprint lang-mma&quot;&gt;
xkcdConvert[BarChart[{1, 10}, ChartLegends -&amp;gt; {&amp;quot;Others&amp;quot;, &amp;quot;XKCD&amp;quot;},
  PlotLabel -&amp;gt; &amp;quot;Popularity of questions on MMA.SE&amp;quot;,
  ChartStyle -&amp;gt; {Red, Green}]]&lt;/pre&gt;



&lt;p&gt;&lt;img alt=&quot;&quot; src=&quot;https://mmaqa.com/qa/?qa=blob&amp;amp;qa_blobid=12357544131964889027&quot; style=&quot;height:264px; width:457px&quot;&gt;&lt;/p&gt;



&lt;hr&gt;


&lt;pre class=&quot;prettyprint lang-mma&quot;&gt;
xkcdConvert[PieChart[{9, 1}, ChartLabels -&amp;gt; {&amp;quot;XKCD&amp;quot;, &amp;quot;Others&amp;quot;},
  PlotLabel -&amp;gt; &amp;quot;Popularity of questions on MMA.SE&amp;quot;]]&lt;/pre&gt;



&lt;p&gt;&lt;img alt=&quot;&quot; src=&quot;https://mmaqa.com/qa/?qa=blob&amp;amp;qa_blobid=18103753310968732201&quot; style=&quot;height:393px; width:370px&quot;&gt;&lt;/p&gt;



&lt;hr&gt;


&lt;pre class=&quot;prettyprint lang-mma&quot;&gt;
xkcdConvert[
 ListLinePlot[RandomInteger[10, 15], PlotMarkers -&amp;gt; Automatic]]&lt;/pre&gt;



&lt;p&gt;&lt;img alt=&quot;&quot; src=&quot;https://mmaqa.com/qa/?qa=blob&amp;amp;qa_blobid=12347487584610064803&quot; style=&quot;height:253px; width:370px&quot;&gt;&lt;/p&gt;



&lt;hr&gt;


&lt;pre class=&quot;prettyprint lang-mma&quot;&gt;
xkcdConvert[BarChart3D[{3, 2, 1}, ChartStyle -&amp;gt; Red, FaceGrids -&amp;gt; None,
  Method -&amp;gt; {&amp;quot;Canvas&amp;quot; -&amp;gt; None}, ViewPoint -&amp;gt; {-2, -4, 1},
  PlotLabel -&amp;gt; &amp;quot;This is just silly&amp;quot;]]&lt;/pre&gt;



&lt;p&gt;&lt;img alt=&quot;&quot; src=&quot;https://mmaqa.com/qa/?qa=blob&amp;amp;qa_blobid=15102084412147181788&quot; style=&quot;height:311px; width:370px&quot;&gt;&lt;/p&gt;



&lt;hr&gt;


&lt;pre class=&quot;prettyprint lang-mma&quot;&gt;
xkcdConvert[
 Plot3D[Exp[-10 (x^2 + y^2)^4], {x, -1, 1}, {y, -1, 1}, 
  MeshStyle -&amp;gt; Thick,
  Boxed -&amp;gt; False, Lighting -&amp;gt; {{&amp;quot;Ambient&amp;quot;, White}},
  PlotLabel -&amp;gt; Framed@&amp;quot;This plot is not\nparticularly useful&amp;quot;]]&lt;/pre&gt;



&lt;p&gt;&lt;img alt=&quot;&quot; src=&quot;https://mmaqa.com/qa/?qa=blob&amp;amp;qa_blobid=2142749085548326406&quot;&gt;&lt;/p&gt;



&lt;hr&gt;


&lt;p&gt;图中使用中文字符时，通常采用&amp;ldquo;迷你简卡通&amp;rdquo;字体，这是科学松鼠会和果壳网常用的做法。
&lt;br&gt;
比如下图：&lt;/p&gt;



&lt;pre class=&quot;prettyprint lang-mma&quot;&gt;
Graphics[{BezierCurve@{{0, 0}, {0, 1}, {1, 1}}, 
   Line[{#, 
       Interpolation[{{{1}, 1, 0}, {{1.25}, 0.75, 0}, {{2}, 2, 
           0}}][#]} &amp;amp; /@ Range[1, 2, 0.01]], 
   ReIm@Exp[I (Pi/2 - #)]/3 + {#, #}/(4 Pi) + {2, 5/3} &amp;amp; /@ 
     Range[0, 4 Pi, Pi/10] // Line, 
   BezierCurve[
    Join[{{3, 3}, {3.2, 3}}, {1, 2} # + {3, 2} &amp;amp; /@ 
      RandomReal[{0, 1}, {20, 2}], {{3.8, 4}, {4, 4}}]]}, 
  Epilog -&amp;gt; 
   xkcdLabel /@ {{&amp;quot;基本语法&amp;quot;, {0.5, 1}, {0, 0.5}}, {&amp;quot;纯函数&amp;quot;, {1.5, 
       1.5}, {-0.5, 1}}, {&amp;quot;模式替换&amp;quot;, {2.5, 2.5}, {-0.5, 
       1}}, {&amp;quot;万物皆式&amp;quot;, {3.5, 2}, {-0.5, -1}}}, Axes -&amp;gt; True, 
  PlotLabel -&amp;gt; &amp;quot;mma学习曲线&amp;quot;, 
  Ticks -&amp;gt; {MapIndexed[{Last@#2 - 0.5, &amp;quot;阶段&amp;quot; ~~ #1} &amp;amp;, 
     ToString /@ {一, 二, 三, 四}], {}}] // xkcdConvert&lt;/pre&gt;



&lt;p&gt;&lt;img alt=&quot;&quot; src=&quot;https://mmaqa.com/qa/?qa=blob&amp;amp;qa_blobid=14806267043290077380&quot; style=&quot;height:403px; width:370px&quot;&gt;&lt;/p&gt;



&lt;p&gt;&amp;nbsp;&lt;/p&gt;



&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description>
<category>SE</category>
<guid isPermaLink="true">https://mmaqa.com/qa/1870/sexkcd?show=1871#a1871</guid>
<pubDate>Fri, 14 Apr 2017 14:44:07 +0000</pubDate>
</item>
</channel>
</rss>