{"id":2007,"date":"2024-01-05T02:40:54","date_gmt":"2024-01-05T10:40:54","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=2007"},"modified":"2024-01-14T15:10:15","modified_gmt":"2024-01-14T23:10:15","slug":"401-add-labels-to-line-plot-in-python","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/401-add-labels-to-line-plot-in-python\/","title":{"rendered":"#401 Add labels to line plot in python"},"content":{"rendered":"<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/401-add-labels-to-line-plot-in-python\/ex401\/\" rel=\"attachment wp-att-2008\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex401.png?resize=640%2C480&#038;ssl=1\" alt=\"\" width=\"640\" height=\"480\" class=\"alignnone size-full wp-image-2008\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex401.png 640w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex401-480x360.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 640px, 100vw\" \/><\/a><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\nplt.clf()\r\n# using some dummy data for this example\r\nn = 25\r\nxs = np.linspace(1, 100, num=n)\r\nys = np.random.normal(loc=3, scale=0.4, size=n)\r\n# 'bo-' means blue color, round points, solid lines\r\nplt.plot(xs,ys,'bo-')\r\n# zip joins x and y coordinates in pairs\r\nfor x,y in zip(xs,ys):\r\n    label = &quot;{:.2f}&quot;.format(y)\r\n    plt.annotate(label, # this is the text\r\n                 (x,y), # these are the coordinates to position the label\r\n                 textcoords=&quot;offset points&quot;, # how to position the text\r\n                 xytext=(10,10), # distance from text to points (x,y)\r\n                 ha='center') # horizontal alignment can be left, right or center\r\n\r\ndef annot_max(x,y, ax=None):\r\n    xmax = x&#x5B;np.argmax(y)]\r\n    ymax = y.max()\r\n    #text= &quot;x={:.3f}, y={:.3f}&quot;.format(xmax, ymax)\r\n    label = &quot;{:.2f}&quot;.format(ymax)\r\n    if not ax:\r\n        ax=plt.gca()\r\n    kw = dict(xycoords='data',textcoords=&quot;offset points&quot;,ha=&quot;center&quot;,bbox=dict(facecolor='none', edgecolor='black', pad=2.0))\r\n    ax.annotate(label, xy=(xmax, ymax), xytext=(10,10),color='red', **kw)\r\n\r\nannot_max(xs,ys)\r\nfor pos in &#x5B;'right', 'top']:\r\n   plt.gca().spines&#x5B;pos].set_visible(False)\r\nplt.savefig(&quot;ex401.png&quot;, dpi=100)\r\nplt.show()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import matplotlib.pyplot as plt import numpy as np plt.clf() # using some dummy data for this example n = 25 xs = np.linspace(1, 100, num=n) ys = np.random.normal(loc=3, scale=0.4, size=n) # &#8216;bo-&#8216; means blue color, round points, solid lines plt.plot(xs,ys,&#8217;bo-&#8216;) # zip joins x and y coordinates in pairs for x,y in zip(xs,ys): label = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2008,"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":[69,91,2],"tags":[],"class_list":["post-2007","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-matplotlib","category-plot","category-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex401.png?fit=640%2C480&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-wn","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1182,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/204-mandelbrot-fractal-using-python-2-2\/","url_meta":{"origin":2007,"position":0},"title":"#206 Triangulation using matplotlib","author":"gantovnik","date":"2021-11-26","format":false,"excerpt":"[code language=\"python\"] import matplotlib.pyplot as plt from matplotlib.tri import Triangulation from matplotlib.patches import Polygon import numpy as np def update_polygon(tri): if tri == -1: points = [0, 0, 0] else: points = triang.triangles[tri] xs = triang.x[points] ys = triang.y[points] polygon.set_xy(np.column_stack([xs, ys])) def on_mouse_move(event): if event.inaxes is None: tri = -1\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\/ex206.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1201,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2\/","url_meta":{"origin":2007,"position":1},"title":"#211 Lorenz attractor","author":"gantovnik","date":"2021-11-27","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt def lorenz(x, y, z, s=10, r=28, b=2.667): \"\"\" Given: x, y, z: a point of interest in three dimensional space s, r, b: parameters defining the lorenz attractor Returns: x_dot, y_dot, z_dot: values of the lorenz attractor's partial derivatives at\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\/ex211.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":73,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/spline-interpolation\/","url_meta":{"origin":2007,"position":2},"title":"Spline interpolation","author":"gantovnik","date":"2018-12-24","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np from scipy.interpolate import spline os.chdir('\/home\/vg\/Downloads\/projects\/ex11') os.getcwd() plt.figure(figsize=(10,8)) xp = np.linspace(0,1,6) yp = np.sqrt(1-xp**2) xi = np.linspace(0,1,100) yi = np.interp(xi,xp,yp) ys = spline(xp,yp,xi) plt.plot(xp,yp,'o',label='given points',lw=2) plt.plot(xi,yi,'--',label='piecewise linear',lw=2) plt.plot(xi,ys,'-',label='spline',lw=2) plt.legend(loc='best') plt.grid() plt.xlabel('x') plt.ylabel('y') plt.title(r'Spline interpolation of $y=\\sqrt{1-x^2}$') plt.axis('scaled') plt.axis([0,1.2,0,1.2]) plt.savefig(\"example11.png\", dpi=100) plt.show()\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example11","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example11.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example11.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example11.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1649,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/11\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-3-2-2-3-2-2-4-2-2-2-2\/","url_meta":{"origin":2007,"position":3},"title":"#313 Spline plot using C# Windows Forms App","author":"gantovnik","date":"2022-11-04","format":false,"excerpt":"Add ScottPlot.WinForms using Manage NuGet packages. Form1.cs [code language=\"csharp\"] using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ScottPlot; namespace Spline { public partial class Form1 : Form { public static class Cubic { \/\/\/ <summary> \/\/\/ Generate a smooth (interpolated) curve that follows the path\u2026","rel":"","context":"In &quot;C#&quot;","block_context":{"text":"C#","link":"https:\/\/gantovnik.com\/bio-tips\/category\/c\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/11\/interpolation.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/11\/interpolation.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/11\/interpolation.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/11\/interpolation.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1980,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/12\/397-spiral-that-goes-around-circular-paraboloid-by-python\/","url_meta":{"origin":2007,"position":4},"title":"#397 Spiral that goes around circular paraboloid by python","author":"gantovnik","date":"2023-12-18","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = plt.axes(projection='3d') # Surface ------------------ # Create the mesh in polar coordinates and compute corresponding Z r0 = 5 r = np.linspace(0, r0, 50) p = np.linspace(0, 2*np.pi, 50) R, P = np.meshgrid(r, p) Z =\u2026","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/12\/ex397.png?fit=640%2C480&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/12\/ex397.png?fit=640%2C480&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/12\/ex397.png?fit=640%2C480&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":210,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/2nd-order-runge-kutta-type-a\/","url_meta":{"origin":2007,"position":5},"title":"#44 2nd-order Runge-Kutta type A using python","author":"gantovnik","date":"2019-01-12","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt os.chdir(r'D:\\projects\\wordpress\\ex44') os.getcwd() #2nd-order Runge-Kutta methods with A=1\/2 (type A) # dy\/dx=exp(-2x)-2y # y(0)=0.1, interval x=[0,2], step size = h=0.2 def feval(funcName, *args): return eval(funcName)(*args) def RK2A(func, yinit, x_range, h): m = len(yinit) n = int((x_range[-1] - x_range[0])\/h) x\u2026","rel":"","context":"In &quot;numerical&quot;","block_context":{"text":"numerical","link":"https:\/\/gantovnik.com\/bio-tips\/category\/numerical\/"},"img":{"alt_text":"example44","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example44-1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example44-1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example44-1.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2007","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=2007"}],"version-history":[{"count":1,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2007\/revisions"}],"predecessor-version":[{"id":2072,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2007\/revisions\/2072"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media\/2008"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=2007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=2007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=2007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}