{"id":1540,"date":"2022-09-02T18:52:57","date_gmt":"2022-09-03T01:52:57","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1540"},"modified":"2024-06-16T00:11:07","modified_gmt":"2024-06-16T07:11:07","slug":"210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-3","status":"publish","type":"post","link":"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\/","title":{"rendered":"#296 y-y plot in python"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/09\/ex295.png?resize=460%2C345&#038;ssl=1\" alt=\"\" width=\"460\" height=\"345\" class=\"alignnone size-full wp-image-1541\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/09\/ex295.png?w=460&amp;ssl=1 460w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/09\/ex295.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/09\/ex295.png?resize=150%2C113&amp;ssl=1 150w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/p>\n<p>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 the plot.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# How to make y-y plots with Matplotlib\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nimport matplotlib as mpl\r\nimport sys\r\n#mpl.style.use(&quot;seaborn&quot;)\r\nmpl.style.use(&quot;default&quot;)\r\nprint(f&#039;Python version {sys.version}&#039;)\r\nprint(f&#039;Matplotlib version {mpl.__version__}&#039;)\r\nprint(f&#039;NumPy version {np.__version__}&#039;)\r\nx = np.arange(0,4*np.pi,0.1)\r\ny1 = np.cos(x)\r\ny2 = np.exp(x)\r\n\r\n# plot function y1\r\nfig, ax = plt.subplots()\r\nax.plot(x,y1)\r\nax.set_title(&#039;sin(x)&#039;)\r\nplt.show()\r\n\r\n# plot function y2\r\nfig, ax = plt.subplots()\r\nax.plot(x,y2)\r\nax.set_title(&#039;exp(x)&#039;)\r\nplt.show()\r\n\r\n# plot two functions\r\nfig, ax = plt.subplots()\r\nax.plot(x,y1)\r\nax.plot(x,y2)\r\nax.set_title(&#039;sin(x) and exp(x)&#039;)\r\nax.legend(&#x5B;&#039;sin(x)&#039;,&#039;exp(x)&#039;])\r\nplt.show()\r\n\r\n# two subplots\r\nfig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(12,4))\r\nax1.plot(x,y1)\r\nax1.set_title(&#039;sin(x)&#039;)\r\nax2.plot(x,y2,&#039;C3&#039;)\r\nax2.set_title(&#039;exp(x)&#039;)\r\nplt.show()\r\n\r\n# y-y plot using twinx\r\nfig, ax1 = plt.subplots()\r\nax1.plot(x,y1)\r\nax1.set_title(&#039;sin(x) and exp(x)&#039;)\r\nax2 = ax1.twinx()\r\nax2.plot(x,y2,&#039;C3&#039;)\r\nplt.show()\r\n\r\n# color each y axis\r\nfig, ax1 = plt.subplots()\r\nax1.plot(x,y1)\r\nax1.set_ylabel(&#039;sin(x)&#039;, color=&#039;C0&#039;)\r\nax1.tick_params(axis=&#039;y&#039;, color=&#039;C0&#039;, labelcolor=&#039;C0&#039;)\r\nax1.set_title(&#039;sin(x) and exp(x)&#039;)\r\nax2 = ax1.twinx()\r\nax2.plot(x,y2,&#039;C3&#039;)\r\nax2.set_ylabel(&#039;exp(x)&#039;, color=&#039;C3&#039;)\r\nax2.tick_params(axis=&#039;y&#039;, color=&#039;C3&#039;, labelcolor=&#039;C3&#039;)\r\nax2.spines&#x5B;&#039;right&#039;].set_color(&#039;C3&#039;)\r\nax2.spines&#x5B;&#039;left&#039;].set_color(&#039;C0&#039;)\r\nplt.show()\r\n\r\n\r\n# add a legends\r\nfig, ax1 = plt.subplots()\r\nax1.plot(x,y1)\r\nax1.set_ylabel(&#039;sin(x)&#039;, color=&#039;C0&#039;)\r\nax1.tick_params(axis=&#039;y&#039;, color=&#039;C0&#039;, labelcolor=&#039;C0&#039;)\r\nax1.set_title(&#039;sin(x) and exp(x)&#039;)\r\nax2 = ax1.twinx()\r\nax2.plot(x,y2,&#039;C3&#039;)\r\nax2.set_ylabel(&#039;exp(x)&#039;, color=&#039;C3&#039;)\r\nax2.tick_params(axis=&#039;y&#039;, color=&#039;C3&#039;, labelcolor=&#039;C3&#039;)\r\nax2.spines&#x5B;&#039;right&#039;].set_color(&#039;C3&#039;)\r\nax2.spines&#x5B;&#039;left&#039;].set_color(&#039;C0&#039;)\r\nfig.legend(&#x5B;&#039;sin(x)&#039;,&#039;exp(x)&#039;], bbox_to_anchor=(0.9, 0.8))\r\nplt.show()\r\n\r\n\r\n# alternative way to add legends\r\nfig, ax1 = plt.subplots()\r\nline1 = ax1.plot(x,y1)\r\nax1.set_ylabel(&#039;sin(x)&#039;, color=&#039;C0&#039;)\r\nax1.tick_params(axis=&#039;y&#039;, color=&#039;C0&#039;, labelcolor=&#039;C0&#039;)\r\nax1.set_title(&#039;sin(x) and exp(x)&#039;)\r\nax2 = ax1.twinx()\r\nline2 = ax2.plot(x,y2,&#039;C3&#039;)\r\nax2.set_ylabel(&#039;exp(x)&#039;, color=&#039;C3&#039;)\r\nax2.tick_params(axis=&#039;y&#039;, color=&#039;C3&#039;, labelcolor=&#039;C3&#039;)\r\nax2.spines&#x5B;&#039;right&#039;].set_color(&#039;C3&#039;)\r\nax2.spines&#x5B;&#039;left&#039;].set_color(&#039;C0&#039;)\r\nlines = line1 + line2\r\nax2.legend(lines, &#x5B;&#039;sin(x)&#039;,&#039;exp(x)&#039;])\r\nplt.savefig(&#039;ex296.png&#039;, dpi=72)\r\nplt.show()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 the plot. # How to [&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_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":"","jetpack_post_was_ever_published":false},"categories":[69,91,2,92],"tags":[70,115,116],"class_list":["post-1540","post","type-post","status-publish","format-standard","hentry","category-matplotlib","category-plot","category-python","category-seaborn","tag-matplotlib","tag-seaborn","tag-y-y-plot"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-oQ","jetpack_likes_enabled":true,"jetpack-related-posts":[{"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":1540,"position":0},"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":2065,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/408-animated-line-plot-in-python\/","url_meta":{"origin":1540,"position":1},"title":"#408 Animated line plot in python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"[code language=\"python\"] import numpy as np from matplotlib import pyplot as plt from matplotlib.animation import FuncAnimation plt.style.use('seaborn-pastel') fig = plt.figure() n=10 ax = plt.axes(xlim=(0, n), ylim=(-2, 2)) line, = ax.plot([], [], c=\"blue\") def init(): line.set_data([], []) return line, def animate(i): n = 10 x = np.linspace(0, n, 1000) y =\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\/2024\/01\/ex408-1.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex408-1.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex408-1.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":82,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/plot-with-an-inset\/","url_meta":{"origin":1540,"position":2},"title":"#12: Plot with an inset in python","author":"gantovnik","date":"2018-12-29","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np os.chdir('\/home\/vg\/Downloads\/projects\/ex12') 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\"$x$\",fontsize=fontsize) ax.set_ylabel(r\"$f(x)$\",fontsize=fontsize) ax=fig.add_axes([0.1,0.15,0.8,0.8],facecolor=\"#f5f5f5\") x = np.linspace(-4,14,1000) plot_and_format_axes(ax,x,f,18) plt.title('Plot with inset') x0,x1=2.5,3.5 ax.axvline(x0,ymax=0.3,color=\"grey\",linestyle=\":\") ax.axvline(x1,ymax=0.3,color=\"grey\",linestyle=\":\") ax_insert=fig.add_axes([0.5,0.5,0.38,0.42],facecolor='none') x=np.linspace(x0,x1,1000) plot_and_format_axes(ax_insert,x,f,14) plt.savefig(\"example12.png\", dpi=100)\u2026","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"example12","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example12.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example12.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example12.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2099,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/410-2d-random-walk-animation-in-python\/","url_meta":{"origin":1540,"position":3},"title":"#410 2D random walk animation in python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"[code language=\"python\"] import numpy import random import matplotlib.pyplot as plt from matplotlib import animation # define some plot variables fig, ax = plt.subplots(figsize=(8,8)) bound = 25 ax.set_xlim(-bound,bound) ax.set_ylim(-bound,bound) # define a numpy array to hold the locations visited on the random walk locations = numpy.zeros((1,2)) # 1 row, 2 columns\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\/2024\/01\/ex410.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex410.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex410.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1938,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/08\/383-horizontal-bar-plot-using-seaborn-library\/","url_meta":{"origin":1540,"position":4},"title":"#383 Horizontal bar plot using seaborn library","author":"gantovnik","date":"2023-08-11","format":false,"excerpt":"[code language=\"python\"] #Horizontal bar plot using seaborn library import matplotlib.pyplot as plt #conda install -c anaconda seaborn import seaborn as sns sns.set_theme(style=\"whitegrid\") def main(): # Initialize the matplotlib figure f, ax = plt.subplots(figsize=(6, 15)) # Load the example car crash dataset crashes = sns.load_dataset(\"car_crashes\").sort_values(\"total\", ascending=False) # Plot the total crashes\u2026","rel":"","context":"In &quot;plot&quot;","block_context":{"text":"plot","link":"https:\/\/gantovnik.com\/bio-tips\/category\/plot\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/08\/ex383.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/08\/ex383.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/08\/ex383.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1923,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/08\/377-stacked-histogram-on-a-log-scale-using-seaborn\/","url_meta":{"origin":1540,"position":5},"title":"#377 Stacked histogram on a log scale using seaborn library","author":"gantovnik","date":"2023-08-10","format":false,"excerpt":"[code language=\"python\"] #Stacked histogram on a log scale import matplotlib.pyplot as plt import matplotlib as mpl #conda install -c anaconda seaborn import seaborn as sns sns.set_theme(style=\"ticks\") def main(): diamonds = sns.load_dataset(\"diamonds\") f, ax = plt.subplots(figsize=(7, 5)) sns.despine(f) sns.histplot( diamonds, x=\"price\", hue=\"cut\", multiple=\"stack\", palette=\"light:m_r\", edgecolor=\".3\", linewidth=.5, log_scale=True, ) ax.xaxis.set_major_formatter(mpl.ticker.ScalarFormatter()) ax.set_xticks([500, 1000,\u2026","rel":"","context":"In &quot;plot&quot;","block_context":{"text":"plot","link":"https:\/\/gantovnik.com\/bio-tips\/category\/plot\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/08\/ex377.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/08\/ex377.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/08\/ex377.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/08\/ex377.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1540","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=1540"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1540\/revisions"}],"predecessor-version":[{"id":2214,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1540\/revisions\/2214"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}