{"id":2131,"date":"2024-02-20T23:20:13","date_gmt":"2024-02-21T07:20:13","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=2131"},"modified":"2024-02-20T23:20:14","modified_gmt":"2024-02-21T07:20:14","slug":"415-tight_layout-in-matplotlib-in-python","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2024\/02\/415-tight_layout-in-matplotlib-in-python\/","title":{"rendered":"#415 tight_layout() in matplotlib in python"},"content":{"rendered":"<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2024\/02\/415-tight_layout-in-matplotlib-in-python\/ex415\/\" rel=\"attachment wp-att-2132\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex415.png?resize=1000%2C300&#038;ssl=1\" alt=\"\" width=\"1000\" height=\"300\" class=\"alignnone size-full wp-image-2132\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex415.png 1000w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex415-980x294.png 980w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex415-480x144.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1000px, 100vw\" \/><\/a><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\nfig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10,3))\r\nxx = np.linspace(1, 20, 1000)\r\nyy = np.log(xx**2 + 1) * np.sin(xx) + xx\r\nax1.plot(xx, yy)\r\nax2.scatter(sorted(xx), sorted(yy), s=1, marker=&quot;.&quot;, color=&quot;green&quot;)\r\nax3.hist(yy, bins=20, color=&quot;red&quot;,edgecolor='black', alpha=.5)\r\nfig.tight_layout()\r\nplt.savefig(&quot;ex415.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, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10,3)) xx = np.linspace(1, 20, 1000) yy = np.log(xx**2 + 1) * np.sin(xx) + xx ax1.plot(xx, yy) ax2.scatter(sorted(xx), sorted(yy), s=1, marker=&quot;.&quot;, color=&quot;green&quot;) ax3.hist(yy, bins=20, color=&quot;red&quot;,edgecolor=&#8217;black&#8217;, alpha=.5) fig.tight_layout() plt.savefig(&quot;ex415.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-2131","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-yn","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":111,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/figure-and-subplots-in-matplotlib\/","url_meta":{"origin":2131,"position":0},"title":"Figure and subplots in matplotlib","author":"gantovnik","date":"2019-01-03","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np from numpy.random import randn os.chdir(r'D:\\data\\scripts\\web1\\ex20') os.getcwd() fig = plt.figure() ax1=fig.add_subplot(2,2,1) ax2=fig.add_subplot(2,2,2) ax3=fig.add_subplot(2,2,3) ax3.plot(randn(50).cumsum(),'k--') ax1.hist(randn(100),bins=20,color='k',alpha=0.3) ax2.scatter(np.arange(30),np.arange(30)+3*randn(30)) fig.suptitle('Figure and subplots') plt.savefig(\"example20.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":"example20","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example20.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example20.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example20.png?resize=525%2C300 1.5x"},"classes":[]},{"id":193,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/double-pendulum\/","url_meta":{"origin":2131,"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":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":2131,"position":2},"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":187,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/coupled-damped-springs\/","url_meta":{"origin":2131,"position":3},"title":"Coupled damped springs","author":"gantovnik","date":"2019-01-09","format":false,"excerpt":"\u00a0 import os import numpy as np import matplotlib.pyplot as plt from scipy import integrate os.chdir(r'D:\\projects\\wordpress\\ex37') os.getcwd() def f(t, y, args): m1, k1, g1, m2, k2, g2 = args return [y[1], - k1\/m1 * y[0] + k2\/m1 * (y[2] - y[0]) - g1\/m1 * y[1], y[3], - k2\/m2 * (y[2]\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example37","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example37.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example37.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example37.png?resize=525%2C300 1.5x"},"classes":[]},{"id":190,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/coupled-damped-springs-jacobian-is-available\/","url_meta":{"origin":2131,"position":4},"title":"Coupled damped springs (Jacobian is available)","author":"gantovnik","date":"2019-01-10","format":false,"excerpt":"\u00a0 import os import numpy as np import matplotlib.pyplot as plt from scipy import integrate os.chdir(r'D:\\projects\\wordpress\\ex38') os.getcwd() def f(t, y, args): m1, k1, g1, m2, k2, g2 = args return [y[1], - k1\/m1 * y[0] + k2\/m1 * (y[2] - y[0]) - g1\/m1 * y[1], y[3], - k2\/m2 * (y[2]\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example38","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example38.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example38.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example38.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1235,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":2131,"position":5},"title":"#221 Streamplot","author":"gantovnik","date":"2021-11-28","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec w = 3 Y, X = np.mgrid[-w:w:100j, -w:w:100j] U = -1 - X**2 + Y V = 1 + X - Y**2 speed = np.sqrt(U**2 + V**2) fig = plt.figure(figsize=(7, 9)) gs = gridspec.GridSpec(nrows=2, ncols=2, height_ratios=[1,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\/ex221.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2131","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=2131"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2131\/revisions"}],"predecessor-version":[{"id":2134,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2131\/revisions\/2134"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=2131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=2131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=2131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}