{"id":2127,"date":"2024-02-20T23:09:42","date_gmt":"2024-02-21T07:09:42","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=2127"},"modified":"2024-02-20T23:09:42","modified_gmt":"2024-02-21T07:09:42","slug":"414-how-to-remove-gaps-between-subplots-in-python","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2024\/02\/414-how-to-remove-gaps-between-subplots-in-python\/","title":{"rendered":"#414 How to remove gaps between subplots in python?"},"content":{"rendered":"<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2024\/02\/414-how-to-remove-gaps-between-subplots-in-python\/ex414\/\" rel=\"attachment wp-att-2128\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex414.png?resize=640%2C480&#038;ssl=1\" alt=\"\" width=\"640\" height=\"480\" class=\"alignnone size-full wp-image-2128\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex414.png 640w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex414-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\nfig, axes = plt.subplots(1, 2, sharey=True)\r\nxx = np.linspace(-10, 10, 1000)\r\n&#x5B;ax.grid() for ax in axes]\r\naxes&#x5B;0].plot(np.sin(xx * xx), color=&quot;blue&quot;)\r\naxes&#x5B;1].plot(np.cos(xx) * xx, color=&quot;red&quot;)\r\nfig.subplots_adjust(wspace=0) # no horizontal space\r\nplt.savefig(&quot;ex414.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 fig, axes = plt.subplots(1, 2, sharey=True) xx = np.linspace(-10, 10, 1000) &#x5B;ax.grid() for ax in axes] axes&#x5B;0].plot(np.sin(xx * xx), color=&quot;blue&quot;) axes&#x5B;1].plot(np.cos(xx) * xx, color=&quot;red&quot;) fig.subplots_adjust(wspace=0) # no horizontal space plt.savefig(&quot;ex414.png&quot;, dpi=100) plt.show()<\/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,91,2],"tags":[],"class_list":["post-2127","post","type-post","status-publish","format-standard","hentry","category-matplotlib","category-plot","category-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-yj","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":136,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/multivariate-interpolation\/","url_meta":{"origin":2127,"position":0},"title":"Multivariate interpolation","author":"gantovnik","date":"2019-01-04","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np from scipy import interpolate os.chdir(r'D:\\data\\scripts\\web1\\ex28') os.getcwd() x = y = np.linspace(-2, 2, 20) def f(x, y): return np.exp(-(x + .5)**2 - 2*(y + .5)**2) - np.exp(-(x - .5)**2 - 2*(y - .5)**2) X, Y = np.meshgrid(x, y) # simulate noisy\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example28","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example28.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example28.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example28.png?resize=525%2C300 1.5x"},"classes":[]},{"id":154,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/inexact-solutions-to-odes\/","url_meta":{"origin":2127,"position":1},"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":1875,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/06\/354-animation-in-python\/","url_meta":{"origin":2127,"position":2},"title":"#354 Animation in python","author":"gantovnik","date":"2023-06-30","format":false,"excerpt":"[code language=\"python\"] # pip install celluloid #%matplotlib qt from matplotlib import pyplot as plt from celluloid import Camera import numpy as np fig, axes = plt.subplots(2) camera = Camera(fig) t = np.linspace(0, 2.0*np.pi, 128, endpoint=False) for i in t: axes[0].plot(t, np.sin(t + i), color='blue') axes[1].plot(t, np.sin(t - i), color='blue') camera.snap()\u2026","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/06\/ex354.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/06\/ex354.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/06\/ex354.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":151,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/direction-fields\/","url_meta":{"origin":2127,"position":3},"title":"Direction fields","author":"gantovnik","date":"2019-01-07","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['text.usetex'] = True import sympy os.chdir(r'D:\\projects\\wordpress\\ex32') 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 = np.linspace(y_lim[0], y_lim[1], 20) if ax\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example32","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example32.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example32.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example32.png?resize=525%2C300 1.5x"},"classes":[]},{"id":166,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/lotka-volterra-equations-for-predator-pray-populations\/","url_meta":{"origin":2127,"position":4},"title":"Lotka-Volterra equations for predator\/pray populations","author":"gantovnik","date":"2019-01-09","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt from scipy import integrate os.chdir(r'D:\\projects\\wordpress\\ex34') os.getcwd() a, b, c, d = 0.4, 0.002, 0.001, 0.7 def f(xy, t): x, y = xy return [a * x - b * x * y, c * x * y - d *\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example34","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example34.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example34.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example34.png?resize=525%2C300 1.5x"},"classes":[]},{"id":93,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/plots-of-nonlinear-functions\/","url_meta":{"origin":2127,"position":5},"title":"Plots of nonlinear functions","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np os.chdir('\/home\/vg\/Downloads\/projects\/ex15') os.getcwd() #define model parameters x=np.linspace(-2,2,1000) #examples of nonlinear functions f1=x**2-x-1 f2=x**3-3*np.sin(x) f3=np.exp(x)-2 f4=1-x**2+np.sin(50\/(1+x**2)) fig,axes=plt.subplots(1,4,figsize=(12,3),sharey=True) for n,f in enumerate([f1,f2,f3,f4]): axes[n].plot(x,f,lw=1.5) axes[n].axhline(0,ls=':',color='k') axes[n].set_ylim(-5,5) axes[n].set_xticks([-2,-1,0,1,2]) axes[n].set_xlabel(r'$x$',fontsize=18) axes[0].set_ylabel(r'$f(x)$',fontsize=18) titles=[r'$f(x)=x^2-x-1$',r'$f(x)=x^3-3\\sin(x)$',r'$f(x)=\\exp(x)-2$', r'$f(x)=\\sin\\left(50\/(1+x^2)\\right)+1-x^2$'] for n,title in enumerate(titles): axes[n].set_title(title) plt.savefig(\"example15.png\", dpi=100) plt.show() plt.close()","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example15","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example15.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example15.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example15.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2127","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=2127"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2127\/revisions"}],"predecessor-version":[{"id":2130,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2127\/revisions\/2130"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=2127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=2127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=2127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}