{"id":1139,"date":"2021-11-22T20:15:43","date_gmt":"2021-11-23T04:15:43","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1139"},"modified":"2021-11-22T20:15:43","modified_gmt":"2021-11-23T04:15:43","slug":"200-plot-in-matplotlib","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/200-plot-in-matplotlib\/","title":{"rendered":"#200 Plot in matplotlib"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# Plot in matplotlib\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nfrom math import pi\r\nx = np.linspace(-2.0*pi,2.0*pi,200)\r\nplt.plot(x,np.sin(x))\r\nsamples=x&#x5B;::2]\r\nplt.plot(samples,np.sin(samples),'r*')\r\nplt.title('Function sin(x) and some points plotted')\r\nplt.grid()\r\nplt.xlabel(&quot;x&quot;)\r\nplt.ylabel(&quot;y&quot;)\r\nplt.savefig('ex200.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\/ex200.png?resize=921%2C633&#038;ssl=1\" alt=\"\" width=\"921\" height=\"633\" class=\"alignnone size-full wp-image-1140\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex200.png 921w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex200-480x330.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 921px, 100vw\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p># Plot in matplotlib import numpy as np import matplotlib.pyplot as plt from math import pi x = np.linspace(-2.0*pi,2.0*pi,200) plt.plot(x,np.sin(x)) samples=x&#x5B;::2] plt.plot(samples,np.sin(samples),&#8217;r*&#8217;) plt.title(&#8216;Function sin(x) and some points plotted&#8217;) plt.grid() plt.xlabel(&quot;x&quot;) plt.ylabel(&quot;y&quot;) plt.savefig(&#8216;ex200.png&#8217;, dpi=72) 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":[2],"tags":[],"class_list":["post-1139","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-in","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1109,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/193-animation-using-python\/","url_meta":{"origin":1139,"position":0},"title":"#193 Animation using python","author":"gantovnik","date":"2021-11-19","format":false,"excerpt":"[code language=\"python\"] # create an animation import numpy as np import matplotlib.pyplot as plt import matplotlib. Animation as manimation n = 1000 x = np.linspace(0, 6*np.pi, n) y = np.sin(x) # Define the meta data for the movie FFMpegWriter = manimation.writers[\"ffmpeg\"] metadata = dict(title=\"Movie Test\", artist=\"Matplotlib\", comment=\"a red circle following\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\/ex193.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":3022,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/442-the-partial-sums-of-the-fourier-series-for-the-square-wave-function-using-python\/","url_meta":{"origin":1139,"position":1},"title":"#442 The partial sums of the Fourier series for the square-wave function using python","author":"gantovnik","date":"2024-07-25","format":false,"excerpt":"import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider nmax = 5 pi = np.pi x = np.linspace(-2*pi, 2*pi, 1001) def f(xarray): y = np.zeros_like(xarray) for ind, x in enumerate(xarray): xmod = x%(2*pi) if xmod<pi: y[ind] = 1 if x%pi==0: y[ind]= np.nan return y def Fourier(x, nmax):\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\/07\/ex442_3.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_3.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_3.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1179,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/204-mandelbrot-fractal-using-python-2\/","url_meta":{"origin":1139,"position":2},"title":"#205 Plots with annotation in matplotlib","author":"gantovnik","date":"2021-11-26","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt plt.fig = plt.figure(1) plt.ax = plt.subplot(111) x = np.linspace(0,2*np.pi,100) # Function that modulates the amplitude of the sin function amod_sin = lambda x: (1.-0.1*np.sin(25*x))*np.sin(x) plt.ax.plot(x,np.sin(x),label = 'sin') plt.ax.plot(x, amod_sin(x), label = 'modsin') annot1=plt.ax.annotate('amplitude modulated\\n curve', (2.1,1.0), (3.2,0.5), arrowprops={'width':2,'color':'k','connectionstyle':'arc3,rad=+0.5','shrink':0.05},verticalalignment='bottom', horizontalalignment='left', fontsize=10,\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\/ex205.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1880,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/356-animation-of-plot-legend-using-python\/","url_meta":{"origin":1139,"position":3},"title":"#356 Animation of plot legend using python","author":"gantovnik","date":"2023-07-01","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 = plt.figure() camera = Camera(fig) t = np.linspace(0, 2 * np.pi, 128, endpoint=False) for i in range(0,41) : p=plt.plot(t, i*np.sin(t)) plt.legend(p, [f'line {i}']) camera.snap() animation = camera.animate()\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\/07\/ex356.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/07\/ex356.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/07\/ex356.gif?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":1139,"position":4},"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":2059,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/407-multiple-axes-animation-using-python\/","url_meta":{"origin":1139,"position":5},"title":"#407 Multiple axes animation using python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"y=sin(x) [code language=\"python\"] import matplotlib.pyplot as plt import numpy as np import matplotlib.animation as animation from matplotlib.patches import ConnectionPatch fig, (axl, axr) = plt.subplots( ncols=2, sharey=True, figsize=(6, 2), gridspec_kw=dict(width_ratios=[1, 3], wspace=0), ) axl.set_aspect(1) axr.set_box_aspect(1 \/ 3) axr.yaxis.set_visible(False) axr.xaxis.set_ticks([0, np.pi, 2 * np.pi], [\"0\", r\"$\\pi$\", r\"$2\\pi$\"]) # draw circle with initial\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\/ex407.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex407.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex407.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1139","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=1139"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1139\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}