{"id":329,"date":"2018-02-10T21:34:04","date_gmt":"2018-02-10T13:34:04","guid":{"rendered":"https:\/\/mmaqa.com\/blog\/?p=329"},"modified":"2018-08-17T00:00:06","modified_gmt":"2018-08-16T16:00:06","slug":"%e3%80%90se%e3%80%91%e5%a6%82%e4%bd%95%e7%94%bb%e5%87%baxkcd%e6%a0%b7%e5%bc%8f%e7%9a%84%e5%9b%be%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/mmaqa.com\/blog\/329\/2018\/02\/10\/%e3%80%90se%e3%80%91%e5%a6%82%e4%bd%95%e7%94%bb%e5%87%baxkcd%e6%a0%b7%e5%bc%8f%e7%9a%84%e5%9b%be%e8%a1%a8\/","title":{"rendered":"\u3010SE\u3011\u5982\u4f55\u753b\u51faxkcd\u6837\u5f0f\u7684\u56fe\u8868"},"content":{"rendered":"<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">\u6587\u7ae0\u76ee\u5f55<\/p><ul class=\"toc_list\"><li><a href=\"#i\">0. \u95ee\u9898\u63cf\u8ff0<\/a><\/li><li><a href=\"#1\">1. \u7cbe\u5f69\u56de\u7b54<\/a><\/li><li><a href=\"#2\">2. \u4e00\u4e9b\u793a\u4f8b\uff1a<\/a><ul><li><a href=\"#21_1\">2.1 \u793a\u4f8b1<\/a><\/li><li><a href=\"#22_2\">2.2 \u793a\u4f8b2<\/a><\/li><li><a href=\"#23_3\">2.3 \u793a\u4f8b3<\/a><\/li><li><a href=\"#24_4\">2.4 \u793a\u4f8b4<\/a><\/li><li><a href=\"#25_5\">2.5 \u793a\u4f8b5<\/a><\/li><li><a href=\"#26_6\">2.6 \u793a\u4f8b6<\/a><\/li><li><a href=\"#27_7\">2.7 \u793a\u4f8b7<\/a><\/li><li><a href=\"#28_8\">2.8 \u793a\u4f8b8<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<h2><span id=\"i\">0. \u95ee\u9898\u63cf\u8ff0<\/span><\/h2>\n<p><strong>\u5982\u4f55\u753b\u51fa xkcd \u6837\u5f0f\u7684\u56fe\u8868\uff0c\u6837\u4f8b\u5982\u4e0b\uff1a<\/strong><\/p>\n<p><img src=\"https:\/\/mmaqa.com\/blog\/wp-content\/uploads\/2018\/02\/1.png\" alt=\"\" \/><\/p>\n<blockquote><p>\n  \u539f\u95ee\u9898\u5730\u5740\uff1ahttps:\/\/mathematica.stackexchange.com\/questions\/11350\/xkcd-style-graphs<br \/>\n  \u6807\u9898\u4ee5\u201c\u3010SE\u3011\u201d\u5f00\u5934\u7684\u95ee\u9898\uff0c\u6765\u81ea StackExchange \u4e2d\u7684\u7ecf\u5178\u95ee\u9898\uff0c\u7531\u5404\u7f51\u53cb\u7ffb\u8bd1\u5e76\u6574\u7406\u3002\u5728\u6b64\u4e0e\u5927\u5bb6\u5171\u4eab\u3002<br \/>\n  \u60a8\u53ef\u4ee5\u5728\u3010\u6587\u7ae0\u5206\u7c7b\u3011\u70b9\u51fb\u3010Stack Exchange\u3011\u5206\u7c7b\uff0c\u9605\u8bfb\u6b64\u7c7b\u6587\u7ae0\u3002\n<\/p><\/blockquote>\n<p><strong>\u672c\u95ee\u9898\u7531\u4ee5\u4e0b\u7f51\u53cb\u7ffb\u8bd1\u6574\u7406\uff1a\u96fe\u972d\uff0c\u74e6\u5c4b\u9752\u8863<\/strong><\/p>\n<hr \/>\n<h2><span id=\"1\">1. \u7cbe\u5f69\u56de\u7b54<\/span><\/h2>\n<blockquote><p>\n  The code below attempts to apply the XKCD style to a variety of plots and charts.<br \/>\n  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.\n<\/p><\/blockquote>\n<p>\u901a\u8fc7\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u7528MMA\u753b\u51faXKCD\u6837\u5f0f\u7684\u5404\u5f0f\u56fe\u8868\u3002<br \/>\n\u5b9e\u73b0\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u9996\u5148\u6539\u53d8MMA\u539f\u56fe\u4e2d\u7684\u56fe\u5f62\uff08\u4f8b\u5982\u7ebf\uff0c\u5b57\u4f53\u7b49\uff09\uff0c\u7136\u540e\u5bf9\u6574\u4e2a\u56fe\u50cf\u505a\u53d8\u5f62\u3002<\/p>\n<blockquote><p>\n  The final function is xkcdConvert which is simply applied to a standard plot or chart.<\/p>\n<p>  The font style and size are set by xkcdStyle which can be changed to your preference.<br \/>\n  I&#8217;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.<\/p>\n<p>  The function xkcdLabel is provided to allow labelling of plot lines using a little callout.<br \/>\n  The usage is xkcdLabel[{str,{x1,y1},{xo,yo}] where str is the label (e.g. a string), {x1,y1} is the position of the callout line and {xo,yo} is the offset determining the relative position of the label.<\/p>\n<p>  The first example demonstrates its usage.\n<\/p><\/blockquote>\n<p>\u901a\u8fc7\u4f7f\u7528\u6700\u7ec8\u7684\u7a0b\u5e8f xkcdConvert\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u7b80\u5355\u7684\u753b\u51faxkcd\u5f0f\u7684\u57fa\u672c\u56fe\u8868\u3002<\/p>\n<p>\u901a\u8fc7\u6539\u53d8xkcdStyle \u4e2d\u7684\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u6539\u53d8\u8f93\u51fa\u7684\u5b57\u4f53\u548c\u5927\u5c0f\u3002<br \/>\n\u56e0\u4e3a\u5bf9\u6574\u4f53\u56fe\u50cf\u505a\u53d8\u5f62\u7684\u65f6\u5019\u540c\u6837\u4f1a\u53d8\u5f62\u5176\u4e2d\u7684\u5b57\u4f53\uff0c\u800c\u4f7f\u7528\u4e86Comic Sans\u5b57\u4f53\u3002<br \/>\n\u672c\u7a0b\u5e8f\u4e2d\u6ca1\u6709\u4f7f\u7528Humor Sans\u7684\u539f\u56e0\u662f\u4e3a\u4e86\u9632\u6b62\u51fa\u73b0\u4e0d\u597d\u8fa8\u8ba4\u7684\u60c5\u51b5\u3002<\/p>\n<p>xkcdLabel \u7684\u529f\u80fd\u662f\u7528\u6765\u5728\u56fe\u4e2d\u65b9\u4fbf\u7684\u6dfb\u52a0\u6807\u7b7e\u3002<br \/>\nxkcdLabel[{str,{x1,y1},{xo,yo}}\u5404\u53c2\u6570\u4e3a\uff1astr\u4ee3\u8868\u8981\u6dfb\u52a0\u7684\u6807\u6ce8\uff0c{x1,y1} \u4e3a\u6dfb\u52a0\u7684\u6807\u6ce8\u7ebf\u7684\u4f4d\u7f6e\uff0c{xo,yo} \u4e3a\u6807\u6ce8\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u51e0\u4e2a\u57fa\u672c\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-mathematica \">xkcdStyle = {FontFamily -&gt; \"Comic Sans MS\", 16};\n\nxkcdLabel[{str_, {x1_, y1_}, {xo_, yo_}}] := Module[{x2, y2},\n   x2 = x1 + xo; y2 = y1 + yo;\n   {Inset[\n     Style[str, xkcdStyle], {x2, y2}, {1.2 Sign[x1 - x2], \n      Sign[y1 - y2] Boole[x1 == x2]}], Thick, \n    BezierCurve[{{0.9 x1 + 0.1 x2, 0.9 y1 + 0.1 y2}, {x1, y2}, {x2, y2}}]}];\n\nxkcdRules = {EdgeForm[ef:Except[None]] :&gt; EdgeForm[Flatten@{ef, Thick, Black}], \n   Style[x_, st_] :&gt; Style[x, xkcdStyle], \n   Pane[s_String] :&gt; Pane[Style[s, xkcdStyle]],\n   {h_Hue, l_Line} :&gt; {Thickness[0.02], White, l, Thick, h, l},\n   Grid[{{g_Graphics, s_String}}] :&gt; Grid[{{g, Style[s, xkcdStyle]}}],\n   Rule[PlotLabel, lab_] :&gt; Rule[PlotLabel, Style[lab, xkcdStyle]]};\n\nxkcdShow[p_] := Show[p, AxesStyle -&gt; Thick, LabelStyle -&gt; xkcdStyle] \/. xkcdRules\n\nxkcdShow[Labeled[p_, rest__]] := \n Labeled[Show[p, AxesStyle -&gt; Thick, LabelStyle -&gt; xkcdStyle], rest] \/. xkcdRules\n\nxkcdDistort[p_] := Module[{r, ix, iy},\n   r = ImagePad[Rasterize@p, 10, Padding -&gt; White];\n   {ix, iy} = \n    Table[RandomImage[{-1, 1}, ImageDimensions@r]~ImageConvolve~\n      GaussianMatrix[10], {2}];\n   ImagePad[ImageTransformation[r, \n     # + 15 {ImageValue[ix, #], ImageValue[iy, #]} &amp;, DataRange -&gt; Full], -5]];\n\nxkcdConvert[x_] := xkcdDistort[xkcdShow[x]]\n\n<\/code><\/pre>\n<hr \/>\n<h2><span id=\"2\">2. \u4e00\u4e9b\u793a\u4f8b\uff1a<\/span><\/h2>\n<h3><span id=\"21_1\">2.1 \u793a\u4f8b1<\/span><\/h3>\n<pre><code class=\"language-mathematica \">f1[x_] := 5 + 50 (1 + Erf[x - 5]);\nf2[x_] := 20 + 30 (1 - Erf[x - 5]);\nxkcdConvert[Plot[{f1[x], f2[x]}, {x, 0, 10},\n  Epilog -&gt; \n   xkcdLabel \/@ {{\"Label 1\", {1, f1[1]}, {1, 30}}, {\"Label 2\", {8, f2[8]}, {0, 30}}},\n  Ticks -&gt; {{{3.5, \"1st Event\"}, {7, \"2nd Event\"}}, Automatic}]]\n<\/code><\/pre>\n<p><img src=\"https:\/\/mmaqa.com\/blog\/wp-content\/uploads\/2018\/02\/2.png\" alt=\"\" \/><\/p>\n<hr \/>\n<h3><span id=\"22_2\">2.2 \u793a\u4f8b2<\/span><\/h3>\n<pre><code class=\"language-mathematica \">xkcdConvert[BarChart[{10, 1}, ChartLabels -&gt; {\"XKCD\", \"Others\"},\n  PlotLabel -&gt; \"Popularity of questions on MMA.SE\",\n  Ticks -&gt; {None, {{1, \"Min\"}, {10, \"Max\"}}}]]\n<\/code><\/pre>\n<p><img src=\"https:\/\/mmaqa.com\/blog\/wp-content\/uploads\/2018\/02\/3.png\" alt=\"\" \/><\/p>\n<hr \/>\n<h3><span id=\"23_3\">2.3 \u793a\u4f8b3<\/span><\/h3>\n<pre><code class=\"language-mathematica \">xkcdConvert[BarChart[{1, 10}, ChartLegends -&gt; {\"Others\", \"XKCD\"},\n  PlotLabel -&gt; \"Popularity of questions on MMA.SE\",\n  ChartStyle -&gt; {Red, Green}]]\n<\/code><\/pre>\n<p><img src=\"https:\/\/mmaqa.com\/blog\/wp-content\/uploads\/2018\/02\/4.png\" alt=\"\" \/><\/p>\n<hr \/>\n<h3><span id=\"24_4\">2.4 \u793a\u4f8b4<\/span><\/h3>\n<pre><code class=\"language-mathematica \">xkcdConvert[PieChart[{9, 1}, ChartLabels -&gt; {\"XKCD\", \"Others\"},\n  PlotLabel -&gt; \"Popularity of questions on MMA.SE\"]]\n<\/code><\/pre>\n<p><img src=\"https:\/\/mmaqa.com\/blog\/wp-content\/uploads\/2018\/02\/5.png\" alt=\"\" \/><\/p>\n<hr \/>\n<h3><span id=\"25_5\">2.5 \u793a\u4f8b5<\/span><\/h3>\n<pre><code class=\"language-mathematica \">xkcdConvert[\n ListLinePlot[RandomInteger[10, 15], PlotMarkers -&gt; Automatic]]\n<\/code><\/pre>\n<p><img src=\"https:\/\/mmaqa.com\/blog\/wp-content\/uploads\/2018\/02\/6.png\" alt=\"\" \/><\/p>\n<hr \/>\n<h3><span id=\"26_6\">2.6 \u793a\u4f8b6<\/span><\/h3>\n<pre><code class=\"language-mathematica \">xkcdConvert[BarChart3D[{3, 2, 1}, ChartStyle -&gt; Red, FaceGrids -&gt; None,\n  Method -&gt; {\"Canvas\" -&gt; None}, ViewPoint -&gt; {-2, -4, 1},\n  PlotLabel -&gt; \"This is just silly\"]]\n<\/code><\/pre>\n<p><img src=\"https:\/\/mmaqa.com\/blog\/wp-content\/uploads\/2018\/02\/7.png\" alt=\"\" \/><\/p>\n<hr \/>\n<h3><span id=\"27_7\">2.7 \u793a\u4f8b7<\/span><\/h3>\n<pre><code class=\"language-mathematica \">xkcdConvert[\n Plot3D[Exp[-10 (x^2 + y^2)^4], {x, -1, 1}, {y, -1, 1}, \n  MeshStyle -&gt; Thick,\n  Boxed -&gt; False, Lighting -&gt; {{\"Ambient\", White}},\n  PlotLabel -&gt; Framed@\"This plot is not\\nparticularly useful\"]]\n<\/code><\/pre>\n<p><img src=\"https:\/\/mmaqa.com\/blog\/wp-content\/uploads\/2018\/02\/8.png\" alt=\"\" \/><\/p>\n<hr \/>\n<h3><span id=\"28_8\">2.8 \u793a\u4f8b8<\/span><\/h3>\n<p>\u56fe\u4e2d\u4f7f\u7528\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u901a\u5e38\u91c7\u7528\u201c\u8ff7\u4f60\u7b80\u5361\u901a\u201d\u5b57\u4f53\uff0c\u8fd9\u662f\u79d1\u5b66\u677e\u9f20\u4f1a\u548c\u679c\u58f3\u7f51\u5e38\u7528\u7684\u505a\u6cd5\u3002<br \/>\n\u6bd4\u5982\u4e0b\u56fe\uff1a<\/p>\n<pre><code class=\"language-mathematica \">Graphics[{BezierCurve@{{0, 0}, {0, 1}, {1, 1}}, \n   Line[{#, \n       Interpolation[{{{1}, 1, 0}, {{1.25}, 0.75, 0}, {{2}, 2, \n           0}}][#]} &amp; \/@ Range[1, 2, 0.01]], \n   ReIm@Exp[I (Pi\/2 - #)]\/3 + {#, #}\/(4 Pi) + {2, 5\/3} &amp; \/@ \n     Range[0, 4 Pi, Pi\/10] \/\/ Line, \n   BezierCurve[\n    Join[{{3, 3}, {3.2, 3}}, {1, 2} # + {3, 2} &amp; \/@ \n      RandomReal[{0, 1}, {20, 2}], {{3.8, 4}, {4, 4}}]]}, \n  Epilog -&gt; \n   xkcdLabel \/@ {{\"\u57fa\u672c\u8bed\u6cd5\", {0.5, 1}, {0, 0.5}}, {\"\u7eaf\u51fd\u6570\", {1.5, \n       1.5}, {-0.5, 1}}, {\"\u6a21\u5f0f\u66ff\u6362\", {2.5, 2.5}, {-0.5, \n       1}}, {\"\u4e07\u7269\u7686\u5f0f\", {3.5, 2}, {-0.5, -1}}}, Axes -&gt; True, \n  PlotLabel -&gt; \"mma\u5b66\u4e60\u66f2\u7ebf\", \n  Ticks -&gt; {MapIndexed[{Last@#2 - 0.5, \"\u9636\u6bb5\" ~~ #1} &amp;, \n     ToString \/@ {\u4e00, \u4e8c, \u4e09, \u56db}], {}}] \/\/ xkcdConvert\n<\/code><\/pre>\n<p><img src=\"https:\/\/mmaqa.com\/blog\/wp-content\/uploads\/2018\/02\/9.png\" alt=\"\" \/><\/p>\n<hr \/>\n<p>\u3010\u5168\u6587\u5b8c\u3011<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u76ee\u5f550. \u95ee\u9898\u63cf\u8ff01. \u7cbe\u5f69\u56de\u7b542. \u4e00\u4e9b\u793a\u4f8b\uff1a2.1 \u793a\u4f8b12.2 \u793a\u4f8b22.3 \u793a\u4f8b32.4 \u793a\u4f8b42 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":330,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"\u65e5\u5fd7\u5934\u56fe":"","bigfa_ding":"3","\u6253\u8d4f":""},"categories":[4],"tags":[13,14,12],"_links":{"self":[{"href":"https:\/\/mmaqa.com\/blog\/wp-json\/wp\/v2\/posts\/329"}],"collection":[{"href":"https:\/\/mmaqa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mmaqa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mmaqa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mmaqa.com\/blog\/wp-json\/wp\/v2\/comments?post=329"}],"version-history":[{"count":0,"href":"https:\/\/mmaqa.com\/blog\/wp-json\/wp\/v2\/posts\/329\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mmaqa.com\/blog\/wp-json\/wp\/v2\/media\/330"}],"wp:attachment":[{"href":"https:\/\/mmaqa.com\/blog\/wp-json\/wp\/v2\/media?parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mmaqa.com\/blog\/wp-json\/wp\/v2\/categories?post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mmaqa.com\/blog\/wp-json\/wp\/v2\/tags?post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}