{"id":1221,"date":"2021-11-28T11:11:24","date_gmt":"2021-11-28T19:11:24","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1221"},"modified":"2021-11-28T11:13:55","modified_gmt":"2021-11-28T19:13:55","slug":"210-parametric-curve-in-3d-2-2-2-2-2-2-2","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2-2-2-2-2-2-2\/","title":{"rendered":"#217 Annotation in matplotlib"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nfrom matplotlib.ticker import AutoMinorLocator, MultipleLocator\r\nnp.random.seed(19680801)\r\nX = np.linspace(0.5, 3.5, 100)\r\nY1 = 3+np.cos(X)\r\nY2 = 1+np.cos(1+X\/0.75)\/2\r\nY3 = np.random.uniform(Y1, Y2, len(X))\r\nfig = plt.figure(figsize=(8, 8))\r\nax = fig.add_subplot(1, 1, 1, aspect=1)\r\n\r\ndef minor_tick(x, pos):\r\n    if not x % 1.0:\r\n        return &quot;&quot;\r\n    return f&quot;{x:.2f}&quot;\r\n\r\nax.xaxis.set_major_locator(MultipleLocator(1.000))\r\nax.xaxis.set_minor_locator(AutoMinorLocator(4))\r\nax.yaxis.set_major_locator(MultipleLocator(1.000))\r\nax.yaxis.set_minor_locator(AutoMinorLocator(4))\r\n# FuncFormatter is created and used automatically\r\nax.xaxis.set_minor_formatter(minor_tick)\r\nax.set_xlim(0, 4)\r\nax.set_ylim(0, 4)\r\nax.tick_params(which='major', width=1.0)\r\nax.tick_params(which='major', length=10)\r\nax.tick_params(which='minor', width=1.0, labelsize=10)\r\nax.tick_params(which='minor', length=5, labelsize=10, labelcolor='0.25')\r\nax.grid(linestyle=&quot;--&quot;, linewidth=0.5, color='.25', zorder=-10)\r\nax.plot(X, Y1, c=(0.25, 0.25, 1.00), lw=2, label=&quot;Blue signal&quot;, zorder=10)\r\nax.plot(X, Y2, c=(1.00, 0.25, 0.25), lw=2, label=&quot;Red signal&quot;)\r\nax.plot(X, Y3, linewidth=0,\r\n        marker='o', markerfacecolor='w', markeredgecolor='k')\r\nax.set_title(&quot;Anatomy of a figure&quot;, fontsize=20, verticalalignment='bottom')\r\nax.set_xlabel(&quot;X axis label&quot;)\r\nax.set_ylabel(&quot;Y axis label&quot;)\r\nax.legend(loc=&quot;upper right&quot;)\r\n\r\ndef circle(x, y, radius=0.15):\r\n    from matplotlib.patches import Circle\r\n    from matplotlib.patheffects import withStroke\r\n    circle = Circle((x, y), radius, clip_on=False, zorder=10, linewidth=1,\r\n                    edgecolor='black', facecolor=(0, 0, 0, .0125),\r\n                    path_effects=&#x5B;withStroke(linewidth=5, foreground='w')])\r\n    ax.add_artist(circle)\r\n\r\ndef text(x, y, text):\r\n    ax.text(x, y, text, backgroundcolor=&quot;white&quot;,\r\n            ha='center', va='top', weight='bold', color='blue')\r\n\r\n# Minor tick\r\ncircle(0.50, -0.10)\r\ntext(0.50, -0.32, &quot;Minor tick label&quot;)\r\n# Major tick\r\ncircle(-0.03, 4.00)\r\ntext(0.03, 3.80, &quot;Major tick&quot;)\r\n# Minor tick\r\ncircle(0.00, 3.50)\r\ntext(0.00, 3.30, &quot;Minor tick&quot;)\r\n# Major tick label\r\ncircle(-0.15, 3.00)\r\ntext(-0.15, 2.80, &quot;Major tick label&quot;)\r\n# X Label\r\ncircle(1.80, -0.27)\r\ntext(1.80, -0.45, &quot;X axis label&quot;)\r\n# Y Label\r\ncircle(-0.27, 1.80)\r\ntext(-0.27, 1.6, &quot;Y axis label&quot;)\r\n# Title\r\ncircle(1.60, 4.13)\r\ntext(1.60, 3.93, &quot;Title&quot;)\r\n# Blue plot\r\ncircle(1.75, 2.80)\r\ntext(1.75, 2.60, &quot;Line\\n(line plot)&quot;)\r\n# Red plot\r\ncircle(1.20, 0.60)\r\ntext(1.20, 0.40, &quot;Line\\n(line plot)&quot;)\r\n# Scatter plot\r\ncircle(3.20, 1.75)\r\ntext(3.20, 1.55, &quot;Markers\\n(scatter plot)&quot;)\r\n# Grid\r\ncircle(3.00, 3.00)\r\ntext(3.00, 2.80, &quot;Grid&quot;)\r\n# Legend\r\ncircle(3.70, 3.80)\r\ntext(3.70, 3.60, &quot;Legend&quot;)\r\n# Axes\r\ncircle(0.5, 0.5)\r\ntext(0.5, 0.3, &quot;Axes&quot;)\r\n# Figure\r\ncircle(-0.3, 0.65)\r\ntext(-0.3, 0.45, &quot;Figure&quot;)\r\ncolor = 'blue'\r\nax.annotate('Spines', xy=(4.0, 0.35), xytext=(3.3, 0.5),\r\n            weight='bold', color=color,\r\n            arrowprops=dict(arrowstyle='-&gt;',\r\n                            connectionstyle=&quot;arc3&quot;,\r\n                            color=color))\r\nax.annotate('', xy=(3.15, 0.0), xytext=(3.45, 0.45),\r\n            weight='bold', color=color,\r\n            arrowprops=dict(arrowstyle='-&gt;',\r\n                            connectionstyle=&quot;arc3&quot;,\r\n                            color=color))\r\nax.text(4.0, -0.4, &quot;Made with matplotlib&quot;,\r\n        fontsize=10, ha=&quot;right&quot;, color='.5')\r\nplt.savefig('ex217.png', dpi=72)\r\nplt.show()\r\n<\/pre>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex217-1.png?resize=576%2C576&#038;ssl=1\" alt=\"\" width=\"576\" height=\"576\" class=\"alignnone size-full wp-image-1224\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex217-1.png 576w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex217-1-480x480.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 576px, 100vw\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: return &quot;&quot; return f&quot;{x:.2f}&quot; ax.xaxis.set_major_locator(MultipleLocator(1.000)) ax.xaxis.set_minor_locator(AutoMinorLocator(4)) ax.yaxis.set_major_locator(MultipleLocator(1.000)) [&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-1221","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-jH","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1540,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/09\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-3\/","url_meta":{"origin":1221,"position":0},"title":"#296 y-y plot in python","author":"gantovnik","date":"2022-09-02","format":false,"excerpt":"y-y plots are a type of line plot where one line corresponds to one y-axis, and another line on the same plot corresponds to a different y-axis. y-y plots typically have one vertical y-axis on the left edge of the plot and one vertical y-axis on the right edge of\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\/2022\/09\/ex295.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":193,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/double-pendulum\/","url_meta":{"origin":1221,"position":1},"title":"#39 Double pendulum using python","author":"gantovnik","date":"2019-01-10","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt from scipy import integrate import sympy os.chdir(r'D:\\projects\\wordpress\\ex39') os.getcwd() t, g, m1, l1, m2, l2 = sympy.symbols(\"t, g, m_1, l_1, m_2, l_2\") theta1, theta2 = sympy.symbols(\"theta_1, theta_2\", cls=sympy.Function) ode1 = sympy.Eq((m1+m2)*l1 * theta1(t).diff(t,t) + m2*l2 * theta2(t).diff(t,t) + m2*l2 * theta2(t).diff(t)**2\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example39","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example39.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example39.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example39.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1984,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/12\/398-find-points-of-intersection-of-two-circles\/","url_meta":{"origin":1221,"position":2},"title":"#398 Find points of intersection of two circles","author":"gantovnik","date":"2023-12-26","format":false,"excerpt":"- how to find an equation of the common chord of two intersected circles. [code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import math def get_intersections(x0, y0, r0, x1, y1, r1): # circle 1: (x0, y0), radius r0 # circle 2: (x1, y1), radius r1 d=math.sqrt((x1-x0)**2 + (y1-y0)**2)\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\/ex398.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\/ex398.png?fit=640%2C480&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/12\/ex398.png?fit=640%2C480&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":101,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/solution-of-system-of-nonlinear-equations\/","url_meta":{"origin":1221,"position":3},"title":"Solution of system of nonlinear equations","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np import scipy os.chdir('\/home\/vg\/Downloads\/projects\/ex17') os.getcwd() def f(x): return [x[1]-x[0]**3-2*x[0]**2+1,x[1]+x[0]**2-1] tol=0.1 a,b=-2,2 x=np.linspace(-3,2,5000) y1=x**3+2*x**2-1 y2=-x**2+1 fig,ax=plt.subplots(figsize=(8,4)) ax.plot(x,y1,'k',lw=1.5) ax.plot(x,y2,'k',lw=1.5) sol1=scipy.optimize.fsolve(f,[-2,2]) sol2=scipy.optimize.fsolve(f,[1,-1]) sol3=scipy.optimize.fsolve(f,[-2,-5]) sols=[sol1,sol2,sol3] colors=['r','b','g'] for idx,s in enumerate(sols): ax.plot(s[0],s[1],colors[idx]+'*',markersize=15) for m in np.linspace(-4,3,80): for n in np.linspace(-20,20,40): x_guess=[m,n] sol=scipy.optimize.fsolve(f,x_guess) idx = (abs(sols-sol)**2).sum(axis=1).argmin() ax.plot(x_guess[0],x_guess[1],colors[idx]+'.')\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example17","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example17.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example17.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example17.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1746,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":1221,"position":4},"title":"#331 Interpolation with Newton&#8217;s polynomial using python","author":"gantovnik","date":"2023-01-04","format":false,"excerpt":"interpolation.py [code language=\"python\"] def interpolation(c,x,x0): # Evaluate Newton's polynomial at x0. # Degree of polynomial n = len(x) - 1 y0 = c[n] for k in range(1,n+1): y0 = c[n-k] + (x0 - x[n-k]) * y0 return y0 def coef(x,y): # Computes the coefficients of Newton's polynomial. # Number of\u2026","rel":"","context":"In &quot;interpolation&quot;","block_context":{"text":"interpolation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/interpolation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex331.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":401,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/02\/neural-network-prediction\/","url_meta":{"origin":1221,"position":5},"title":"#60 Neural network prediction using python neurolab","author":"gantovnik","date":"2019-02-19","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib.pyplot as plt import numpy as np import neurolab as nl os.chdir(r'D:\\projects\\wordpress\\ex60') os.getcwd() #create train sets x=np.linspace(-10,10,60) y=np.cos(x)*0.9 size=len(x) x_train=x.reshape(size,1) y_train=y.reshape(size,1) #create network with 4 layers and randomly initiate d=[[1,1],[45,1],[45,45,1],[45,45,45,1]] for i in range(4): net=nl.net.newff([[-10,10]],d[i]) train_net=nl.train.train_gd(net,x_train,y_train,epochs=1000,show=100) outp=net.sim(x_train) plt.subplot(2,1,1) plt.grid(True) plt.plot(train_net) plt.title('Hidden Layers: ' + str(i))\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\/2019\/02\/example69_3.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_3.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_3.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1221","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=1221"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1221\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}