{"id":82,"date":"2018-12-29T09:05:21","date_gmt":"2018-12-29T09:05:21","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=82"},"modified":"2024-06-26T03:40:52","modified_gmt":"2024-06-26T10:40:52","slug":"plot-with-an-inset","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/plot-with-an-inset\/","title":{"rendered":"#12: Plot with an inset in python"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"  wp-image-83 aligncenter\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example12.png?resize=591%2C473\" alt=\"example12\" width=\"591\" height=\"473\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example12.png?w=1000&amp;ssl=1 1000w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example12.png?resize=300%2C240&amp;ssl=1 300w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example12.png?resize=768%2C614&amp;ssl=1 768w\" sizes=\"(max-width: 591px) 100vw, 591px\" \/><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\nimport matplotlib as mpl\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\nos.chdir(&#039;\/home\/vg\/Downloads\/projects\/ex12&#039;)\r\nos.getcwd()\r\nfig = plt.figure(figsize=(10,8))\r\n\r\ndef f(x):\r\n    return 1\/(1+x**2) + 0.1\/(1+((3-x)\/0.1)**2)\r\n\r\ndef plot_and_format_axes(ax,x,f,fontsize):\r\n    ax.plot(x,f(x),linewidth=2)\r\n    ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(5))\r\n    ax.yaxis.set_major_locator(mpl.ticker.MaxNLocator(4))\r\n    ax.set_xlabel(r&quot;$x$&quot;,fontsize=fontsize)\r\n    ax.set_ylabel(r&quot;$f(x)$&quot;,fontsize=fontsize)\r\n\r\nax=fig.add_axes(&#x5B;0.1,0.15,0.8,0.8],facecolor=&quot;#f5f5f5&quot;)\r\nx = np.linspace(-4,14,1000)\r\nplot_and_format_axes(ax,x,f,18)\r\nplt.title(&#039;Plot with inset&#039;)\r\nx0,x1=2.5,3.5\r\nax.axvline(x0,ymax=0.3,color=&quot;grey&quot;,linestyle=&quot;:&quot;)\r\nax.axvline(x1,ymax=0.3,color=&quot;grey&quot;,linestyle=&quot;:&quot;)\r\nax_insert=fig.add_axes(&#x5B;0.5,0.5,0.38,0.42],facecolor=&#039;none&#039;)\r\nx=np.linspace(x0,x1,1000)\r\nplot_and_format_axes(ax_insert,x,f,14)\r\nplt.savefig(&quot;example12.png&quot;, dpi=100)\r\nplt.show()\r\nplt.close()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import os import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np os.chdir(&#039;\/home\/vg\/Downloads\/projects\/ex12&#039;) os.getcwd() fig = plt.figure(figsize=(10,8)) def f(x): return 1\/(1+x**2) + 0.1\/(1+((3-x)\/0.1)**2) def plot_and_format_axes(ax,x,f,fontsize): ax.plot(x,f(x),linewidth=2) ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(5)) ax.yaxis.set_major_locator(mpl.ticker.MaxNLocator(4)) ax.set_xlabel(r&quot;$x$&quot;,fontsize=fontsize) ax.set_ylabel(r&quot;$f(x)$&quot;,fontsize=fontsize) ax=fig.add_axes(&#x5B;0.1,0.15,0.8,0.8],facecolor=&quot;#f5f5f5&quot;) x = np.linspace(-4,14,1000) plot_and_format_axes(ax,x,f,18) plt.title(&#039;Plot with inset&#039;) x0,x1=2.5,3.5 ax.axvline(x0,ymax=0.3,color=&quot;grey&quot;,linestyle=&quot;:&quot;) ax.axvline(x1,ymax=0.3,color=&quot;grey&quot;,linestyle=&quot;:&quot;) ax_insert=fig.add_axes(&#x5B;0.5,0.5,0.38,0.42],facecolor=&#039;none&#039;) x=np.linspace(x0,x1,1000) plot_and_format_axes(ax_insert,x,f,14) plt.savefig(&quot;example12.png&quot;, dpi=100) plt.show() plt.close()<\/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":[69,2],"tags":[70,3],"class_list":["post-82","post","type-post","status-publish","format-standard","hentry","category-matplotlib","category-python","tag-matplotlib","tag-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-1k","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":154,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/inexact-solutions-to-odes\/","url_meta":{"origin":82,"position":0},"title":"Inexact solutions to ODEs","author":"gantovnik","date":"2019-01-08","format":false,"excerpt":"\u00a0 import os import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl import sympy from IPython.display import display sympy.init_printing() mpl.rcParams['text.usetex'] = True import sympy os.chdir(r'D:\\projects\\wordpress\\ex33') os.getcwd() def plot_direction_field(x, y_x, f_xy, x_lim=(-5, 5), y_lim=(-5, 5), ax=None): f_np = sympy.lambdify((x, y_x), f_xy, 'numpy') x_vec = np.linspace(x_lim[0], x_lim[1], 20) y_vec\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example33","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example33.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example33.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example33.png?resize=525%2C300 1.5x"},"classes":[]},{"id":117,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/optimization-with-contraints\/","url_meta":{"origin":82,"position":1},"title":"#22: Optimization with constraints using SciPy in python","author":"gantovnik","date":"2019-01-03","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib.pyplot as plt import numpy as np from scipy.optimize import minimize os.chdir(r'D:\\data\\scripts\\web1\\ex22') os.getcwd() def f(X): return (X[0]-1)2 + (X[1]-1)2 def g(X): return X[1]-1.75-(X[0]-0.75)**4 def func_X_Y_to_XY(f, X, Y): s = np.shape(X) return f(np.vstack([X.ravel(), Y.ravel()])).reshape(*s) x_opt=minimize(f,(0,0),method='BFGS').x print(x_opt) constraints = [dict(type='ineq', fun=g)] x_cons_opt = minimize(f, (0, 0), method='SLSQP',\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example22.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example22.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example22.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1955,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/08\/389-calculating-pi-with-monte-carlo-simulation\/","url_meta":{"origin":82,"position":2},"title":"#389 Calculating pi with Monte Carlo Simulation","author":"gantovnik","date":"2023-08-27","format":false,"excerpt":"[code language=\"python\"] # Calculating pi with Monte Carlo Simulation import random import numpy as np import matplotlib.pyplot as plt random.seed(2021) pi_values = list() num_experiments = 1000 num_rounds = 20 num_points = 1000 edge = 10 for r in range(num_rounds): for p in range(num_experiments): in_circle = 0 for g in range(num_points):\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\/08\/ex389_2.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/08\/ex389_2.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/08\/ex389_2.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/08\/ex389_2.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/08\/ex389_2.png?resize=1050%2C600&ssl=1 3x"},"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":82,"position":3},"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":104,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/unconstrained-multivariate-optimization\/","url_meta":{"origin":82,"position":4},"title":"Unconstrained multivariate optimization","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np import scipy import sympy os.chdir('\/home\/vg\/Downloads\/projects\/ex18') os.getcwd() x1,x2=sympy.symbols(\"x_1,x_2\") f_sym=(x1-1)**4 + 5*(x2-1)**2 - 2*x1*x2 fprime_sym=[f_sym.diff(x_) for x_ in (x1,x2)] sympy.Matrix(fprime_sym) fhess_sym=[[f_sym.diff(x1_,x2_) for x1_ in (x1,x2)] for x2_ in (x1,x2)] sympy.Matrix(fhess_sym) f_lmbda=sympy.lambdify((x1,x2),f_sym,'numpy') fprime_lmbda=sympy.lambdify((x1,x2),fprime_sym,'numpy') fhess_lmbda=sympy.lambdify((x1,x2),fhess_sym,'numpy') def func_XY_to_xy(f): return lambda X: np.array(f(X[0],X[1])) f=func_XY_to_xy(f_lmbda) fprime=func_XY_to_xy(fprime_lmbda) fhess=func_XY_to_xy(fhess_lmbda)\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example18","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example18.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example18.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example18.png?resize=525%2C300 1.5x"},"classes":[]},{"id":495,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/05\/optimization-with-constraints\/","url_meta":{"origin":82,"position":5},"title":"#72 Optimization with constraints using scipy in python","author":"gantovnik","date":"2020-05-03","format":false,"excerpt":"[code language=\"python\"] import os from scipy.optimize import minimize import matplotlib.pyplot as plt import numpy as np os.chdir(r'D:\\projects\\wordpress\\ex72') os.getcwd() def f(X): x, y = X return (x - 1)2 + (y - 1)2 def func_X_Y_to_XY(f, X, Y): #Wrapper for f(X, Y) -> f([X, Y]) s = np.shape(X) return f(np.vstack([X.ravel(), Y.ravel()])).reshape(*s) x_opt\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"ex72","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/ex72.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/ex72.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/ex72.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/82","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=82"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"predecessor-version":[{"id":2224,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/82\/revisions\/2224"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}