{"id":5964,"date":"2016-02-08T19:32:35","date_gmt":"2016-02-08T18:32:35","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=5964"},"modified":"2016-02-08T19:34:36","modified_gmt":"2016-02-08T18:34:36","slug":"animated-heart-using-r","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=5964","title":{"rendered":"Animated heart using R"},"content":{"rendered":"<p>While waiting for the rain to stop before heading home, I started messing around with the heart equation described in an <a href=\"https:\/\/www.walkingrandomly.com\/?p=4030\">old WalkingRandomly post<\/a>. Playing code golf with myself, I worked to get the code tweetable. In Python:<\/p>\n<blockquote class=\"twitter-tweet\" data-lang=\"en\">\n<p lang=\"en\" dir=\"ltr\">from pylab import *<br \/>x=r_[-2:2:0.001]<br \/>show(plot((sqrt(cos(x))*cos(200*x)+sqrt(abs(x))-0.7)*(4-x*x)**0.01)) <a href=\"https:\/\/t.co\/gbOTbYSaIG\">pic.twitter.com\/gbOTbYSaIG<\/a><\/p>\n<p>&mdash; Mike Croucher (@walkingrandomly) <a href=\"https:\/\/twitter.com\/walkingrandomly\/status\/696744691715465216\">February 8, 2016<\/a><\/p><\/blockquote>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<p>In R: <\/p>\n<blockquote class=\"twitter-tweet\" data-lang=\"en\">\n<p lang=\"en\" dir=\"ltr\">x=seq(-2,2,0.001)<br \/>y=Re((sqrt(cos(x))*cos(200*x)+sqrt(abs(x))-0.7)*(4-x*x)^0.01)<br \/>plot(x,y)<a href=\"https:\/\/twitter.com\/hashtag\/rstats?src=hash\">#rstats<\/a> <a href=\"https:\/\/t.co\/trpgEnNna4\">pic.twitter.com\/trpgEnNna4<\/a><\/p>\n<p>&mdash; Mike Croucher (@walkingrandomly) <a href=\"https:\/\/twitter.com\/walkingrandomly\/status\/696747727909093378\">February 8, 2016<\/a><\/p><\/blockquote>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<p>I liked the look of the default plot in R so animated it by turning 200 into a parameter that ranged from 1 to 200. The result was this animation: <\/p>\n<blockquote class=\"twitter-tweet\" data-lang=\"en\">\n<p lang=\"en\" dir=\"ltr\">Finding this animation based on previous tweets oddly mesmerising <a href=\"https:\/\/twitter.com\/hashtag\/rstats?src=hash\">#rstats<\/a> <a href=\"https:\/\/t.co\/e3q6lZqWcP\">pic.twitter.com\/e3q6lZqWcP<\/a><\/p>\n<p>&mdash; Mike Croucher (@walkingrandomly) <a href=\"https:\/\/twitter.com\/walkingrandomly\/status\/696761318414622724\">February 8, 2016<\/a><\/p><\/blockquote>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<p>The code for the above isn&#8217;t quite tweetable:<\/p>\n<pre>\r\noptions(warn=-1)\r\nfor(num in seq(1,200,1))\r\n{\r\n    filename = paste(\"rplot\" ,sprintf(\"%03d\", num),'.jpg',sep='')\r\n    jpeg(filename)\r\n    x=seq(-2,2,0.001)\r\n    y=Re((sqrt(cos(x))*cos(num*x)+sqrt(abs(x))-0.7)*(4-x*x)^0.01)\r\n    plot(x,y,axes=FALSE,ann=FALSE)\r\n    dev.off()\r\n}\r\n<\/pre>\n<p>This produces a lot of .jpg files which I turned into the animated gif with ImageMagick:<\/p>\n<pre>\r\nconvert -delay 12 -layers OptimizeTransparency -colors 8 -loop 0 *.jpg animated.gif \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>While waiting for the rain to stop before heading home, I started messing around with the heart equation described in an old WalkingRandomly post. Playing code golf with myself, I worked to get the code tweetable. In Python: from pylab import *x=r_[-2:2:0.001]show(plot((sqrt(cos(x))*cos(200*x)+sqrt(abs(x))-0.7)*(4-x*x)**0.01)) pic.twitter.com\/gbOTbYSaIG &mdash; Mike Croucher (@walkingrandomly) February 8, 2016 In R: x=seq(-2,2,0.001)y=Re((sqrt(cos(x))*cos(200*x)+sqrt(abs(x))-0.7)*(4-x*x)^0.01)plot(x,y)#rstats pic.twitter.com\/trpgEnNna4 &mdash; [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7,31,36,15],"tags":[],"class_list":["post-5964","post","type-post","status-publish","format-standard","hentry","category-programming","category-python","category-r","category-walking-randomly"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-1yc","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5964"}],"version-history":[{"count":3,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5964\/revisions"}],"predecessor-version":[{"id":5968,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5964\/revisions\/5968"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}