{"id":383,"date":"2019-02-12T23:50:27","date_gmt":"2019-02-13T07:50:27","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=383"},"modified":"2021-11-12T15:07:43","modified_gmt":"2021-11-12T23:07:43","slug":"annotating-text","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2019\/02\/annotating-text\/","title":{"rendered":"#55 Annotating text in matplotlib plot"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\nos.chdir(r'D:\\projects\\wordpress\\ex55')\r\nos.getcwd()\r\n\r\nfig = plt.figure()\r\nax = fig.add_subplot(111)\r\nt = np.arange(0.0, 5.0, 0.01)\r\ns = np.cos(2*np.pi*t)\r\nline, = ax.plot(t, s, lw=2)\r\nax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),\r\n            arrowprops=dict(facecolor='red', shrink=0.05),\r\n            )\r\n\r\nax.set_ylim(-2,2)\r\nplt.savefig(&quot;example55.png&quot;, dpi=100)\r\nplt.show()\r\n\r\nfig = plt.figure()\r\nax = fig.add_subplot(111, polar=True)\r\nr = np.arange(0,1,0.001)\r\ntheta = 2*2*np.pi*r\r\nline, = ax.plot(theta, r, color='#ee8d18', lw=3)\r\n\r\nind = 800\r\nthisr, thistheta = r&#x5B;ind], theta&#x5B;ind]\r\nax.plot(&#x5B;thistheta], &#x5B;thisr], 'o')\r\nax.annotate('a polar annotation',\r\n            xy=(thistheta, thisr),  # theta, radius\r\n            xytext=(0.05, 0.05),    # fraction, fraction\r\n            textcoords='figure fraction',\r\n            arrowprops=dict(facecolor='red', shrink=0.05),\r\n            horizontalalignment='left',\r\n            verticalalignment='bottom',\r\n            )\r\n\r\nplt.savefig(&quot;example55_1.png&quot;, dpi=100)\r\nplt.show()\r\nplt.close()\r\n<\/pre>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example64.png?resize=600%2C400&#038;ssl=1\" alt=\"example64\" width=\"600\" height=\"400\" class=\"alignnone size-full wp-image-384\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example64.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example64.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example64_1.png?resize=600%2C400&#038;ssl=1\" alt=\"example64_1\" width=\"600\" height=\"400\" class=\"alignnone size-full wp-image-385\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example64_1.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example64_1.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>import os import matplotlib.pyplot as plt import numpy as np os.chdir(r&#8217;D:\\projects\\wordpress\\ex55&#8242;) os.getcwd() fig = plt.figure() ax = fig.add_subplot(111) t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = ax.plot(t, s, lw=2) ax.annotate(&#8216;local max&#8217;, xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor=&#8217;red&#8217;, shrink=0.05), ) ax.set_ylim(-2,2) plt.savefig(&quot;example55.png&quot;, dpi=100) plt.show() fig = plt.figure() ax = fig.add_subplot(111, polar=True) r = np.arange(0,1,0.001) theta [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-383","post","type-post","status-publish","format-standard","hentry","category-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-6b","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1229,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":383,"position":0},"title":"#219 Polar plot legend","author":"gantovnik","date":"2021-11-28","format":false,"excerpt":"[code language=\"python\"] import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(projection=\"polar\", facecolor=\"lightgoldenrodyellow\") r = np.linspace(0, 3, 301) theta = 2 * np.pi * r ax.plot(theta, r, color=\"tab:orange\", lw=3, label=\"a line\") ax.plot(0.5 * theta, r, color=\"tab:blue\", ls=\"--\", lw=3, label=\"another line\") ax.tick_params(grid_color=\"palegoldenrod\") # For polar axes, it\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex219.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1238,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":383,"position":1},"title":"#222 Xkcd-like plots","author":"gantovnik","date":"2021-11-28","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt with plt.xkcd(): fig = plt.figure() ax = fig.add_axes((0.1, 0.2, 0.8, 0.7)) ax.spines.right.set_color('none') ax.spines.top.set_color('none') ax.set_xticks([]) ax.set_yticks([]) ax.set_ylim([-30, 10]) data = np.ones(100) data[70:] -= np.arange(30) ax.annotate('THE DAY I REALIZED\\nI COULD COOK BACON\\nWHENEVER I WANTED', xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10)) ax.plot(data) ax.set_xlabel('time') ax.set_ylabel('my\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex222_2.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1221,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2-2-2-2-2-2-2\/","url_meta":{"origin":383,"position":2},"title":"#217 Annotation in matplotlib","author":"gantovnik","date":"2021-11-28","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt from matplotlib.ticker import AutoMinorLocator, MultipleLocator np.random.seed(19680801) X = np.linspace(0.5, 3.5, 100) Y1 = 3+np.cos(X) Y2 = 1+np.cos(1+X\/0.75)\/2 Y3 = np.random.uniform(Y1, Y2, len(X)) fig = plt.figure(figsize=(8, 8)) ax = fig.add_subplot(1, 1, 1, aspect=1) def minor_tick(x, pos): if not x % 1.0:\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex217-1.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex217-1.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex217-1.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":96,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/bisection-method\/","url_meta":{"origin":383,"position":3},"title":"#16 Bisection method using python","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib.pyplot as plt import numpy as np os.chdir('\/home\/vg\/Downloads\/projects\/ex16') os.getcwd() f = lambda x: np.exp(x)-2 tol=0.1 a,b=-2,2 x=np.linspace(-2.1,2.1,1000) fig,ax=plt.subplots(1,1,figsize=(12,4)) ax.plot(x,f(x),lw=1.5) ax.axhline(0,ls=':',color='k') ax.set_xticks([-2,-1,0,1,2]) ax.set_xlabel(r'$x$',fontsize=18) ax.set_ylabel(r'$f(x)$',fontsize=18) fa,fb=f(a),f(b) ax.plot(a,fa,'ko') ax.plot(b,fb,'ko') ax.text(a,fa+0.5,r\"$a$\",ha='center',fontsize=18) ax.text(b,fb+0.5,r\"$b$\",ha='center',fontsize=18) n=1 while b-a &gt; tol: m=a+(b-a)\/2 fm=f(m) ax.plot(m,fm,'ko') ax.text(m,fm-0.5,r\"$m_%d$\" % n,ha='center') n=n+1 if (np.sign(fa)==np.sign(fm)): a,fa=m,fm else: b,fb=m,fm\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"example16","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example16.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example16.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example16.png?resize=525%2C300 1.5x"},"classes":[]},{"id":654,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/09\/107-tsa-data-2020-vs-2019\/","url_meta":{"origin":383,"position":4},"title":"#107: TSA Data 2020 vs 2019","author":"gantovnik","date":"2020-09-28","format":false,"excerpt":"#107: TSA Data 2020 vs 2019 The TSA has started to publish the daily volume of passengers going through checkpoints on its website. The data set also includes the numbers from 2019 in order to measure the impact of travel as a result of COVID-19. https:\/\/www.tsa.gov\/coronavirus\/passenger-throughput [code language=\"python\"] from bs4\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/09\/ex107.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/09\/ex107.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/09\/ex107.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/09\/ex107.png?resize=700%2C400 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/09\/ex107.png?resize=1050%2C600 3x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/09\/ex107.png?resize=1400%2C800 4x"},"classes":[]},{"id":1226,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2\/","url_meta":{"origin":383,"position":5},"title":"#218 Polar plot","author":"gantovnik","date":"2021-11-28","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt r = np.arange(0, 2, 0.01) theta = 2 * np.pi * r fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ax.plot(theta, r) ax.set_rmax(2) ax.set_rticks([0.5, 1, 1.5, 2]) # Less radial ticks ax.set_rlabel_position(-22.5) # Move radial labels away from plotted line ax.grid(True) ax.set_title(\"A line\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex218.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=383"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/383\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}