{"id":1188,"date":"2021-11-27T04:26:01","date_gmt":"2021-11-27T12:26:01","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1188"},"modified":"2024-07-14T16:34:52","modified_gmt":"2024-07-14T23:34:52","slug":"204-mandelbrot-fractal-using-python-2-2-2-2","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/204-mandelbrot-fractal-using-python-2-2-2-2\/","title":{"rendered":"#208 Scatter plot with histograms using python"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex208.png?resize=576%2C576&#038;ssl=1\" alt=\"\" width=\"576\" height=\"576\" class=\"alignnone size-full wp-image-1189\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex208.png 576w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex208-480x480.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 576px, 100vw\" \/><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n&lt;h1&gt;Fixing random state for reproducibility&lt;\/h1&gt;\n\nnp.random.seed(19680801)\n\n&lt;h1&gt;some random data&lt;\/h1&gt;\n\nx = np.random.randn(1000)\ny = np.random.randn(1000)\n\ndef scatter_hist(x, y, ax, ax_histx, ax_histy):\n    # no labels\n    ax_histx.tick_params(axis=&amp;quot;x&amp;quot;, labelbottom=False)\n    ax_histy.tick_params(axis=&amp;quot;y&amp;quot;, labelleft=False)\n    # the scatter plot:\n    ax. Scatter(x, y,s=3)\n    # now determine nice limits by hand:\n    binwidth = 0.25\n    xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))\n    lim = (int(xymax\/binwidth) + 1) * binwidth\n    bins = np.arange(-lim, lim + binwidth, binwidth)\n    ax_histx.hist(x, bins=bins)\n    ax_histy.hist(y, bins=bins, orientation=&amp;#039;horizontal&amp;#039;)\n\n&lt;h1&gt;definitions for the axes&lt;\/h1&gt;\n\nleft, width = 0.1, 0.65\nbottom, height = 0.1, 0.65\nspacing = 0.005\nrect_scatter = &#x5B;left, bottom, width, height]\nrect_histx = &#x5B;left, bottom + height + spacing, width, 0.2]\nrect_histy = &#x5B;left + width + spacing, bottom, 0.2, height]\n\n&lt;h1&gt;start with a square Figure&lt;\/h1&gt;\n\nfig = plt.figure(figsize=(8, 8))\nax = fig.add_axes(rect_scatter)\nax_histx = fig.add_axes(rect_histx, sharex=ax)\nax_histy = fig.add_axes(rect_histy, sharey=ax)\n\n&lt;h1&gt;use the previously defined function&lt;\/h1&gt;\n\nscatter_hist(x, y, ax, ax_histx, ax_histy)\nplt.savefig(&amp;#039;ex208.png&amp;#039;, dpi=72)\nplt.show()\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import numpy as np import matplotlib.pyplot as plt &lt;h1&gt;Fixing random state for reproducibility&lt;\/h1&gt; np.random.seed(19680801) &lt;h1&gt;some random data&lt;\/h1&gt; x = np.random.randn(1000) y = np.random.randn(1000) def scatter_hist(x, y, ax, ax_histx, ax_histy): # no labels ax_histx.tick_params(axis=&amp;quot;x&amp;quot;, labelbottom=False) ax_histy.tick_params(axis=&amp;quot;y&amp;quot;, labelleft=False) # the scatter plot: ax. Scatter(x, y,s=3) # now determine nice limits by hand: binwidth = 0.25 xymax = [&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_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":[127],"class_list":["post-1188","post","type-post","status-publish","format-standard","hentry","category-python","tag-histograms"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-ja","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":85,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/subplots-in-matplotlib\/","url_meta":{"origin":1188,"position":0},"title":"#13 Subplots in matplotlib","author":"gantovnik","date":"2018-12-29","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib.pyplot as plt import numpy as np os.chdir('\/home\/vg\/Downloads\/projects\/ex13') os.getcwd() fig,axes = plt.subplots(2,2,figsize=(6,6),sharex=True,sharey=True,squeeze=False) x1=np.random.randn(100) x2=np.random.randn(100) axes[0,0].set_title(\"Uncorrelated\") axes[0,0].scatter(x1,x2) axes[0,1].set_title(\"Weakly positively correlated\") axes[0,1].scatter(x1,x1+x2) axes[1,0].set_title(\"Weakly negatively correlated\") axes[1,0].scatter(x1,-x1+x2) axes[1,1].set_title(\"Strongly correlated\") axes[1,1].scatter(x1,x1+0.15*x2) axes[1,1].set_xlabel(\"x\") axes[1,0].set_xlabel(\"x\") axes[0,0].set_ylabel(\"y\") axes[1,0].set_ylabel(\"y\") plt.subplots_adjust(left=0.1,right=0.95,bottom=0.1,top=0.95,wspace=0.1,hspace=0.2) plt.savefig(\"example13.png\", dpi=100) plt.show() plt.close() [\/code]","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example13","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2104,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/411-clustering-using-dbscan-algorithm-in-sklearn-cluster-in-python\/","url_meta":{"origin":1188,"position":1},"title":"#411 Clustering using DBSCAN algorithm in sklearn.cluster in python","author":"gantovnik","date":"2024-01-18","format":false,"excerpt":"DBSCAN works by finding core points that have many data points within a given radius. Once the core is defined, the process is iteratively computed until there are no more core points definable within the maximum radius. This algorithm does exceptionally well compared to kmeans where there is noise present\u2026","rel":"","context":"In &quot;cluster&quot;","block_context":{"text":"cluster","link":"https:\/\/gantovnik.com\/bio-tips\/category\/cluster\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex411.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2800,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/437-particle-swarm-optimization-using-python\/","url_meta":{"origin":1188,"position":2},"title":"#437 Particle swarm optimization using python","author":"gantovnik","date":"2024-07-20","format":false,"excerpt":"import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation def f(x,y): return (x-3.14)**2 + (y-2.72)**2 + np.sin(3*x+1.41) + np.sin(4*y-1.73) # Compute and plot the function in 3D within [0,5]x[0,5] x, y = np.array(np.meshgrid(np.linspace(0,5,100), np.linspace(0,5,100))) z = f(x, y) # Find the global minimum x_min = x.ravel()[z.argmin()] y_min\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\/ex437.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex437.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex437.gif?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex437.gif?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2131,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/02\/415-tight_layout-in-matplotlib-in-python\/","url_meta":{"origin":1188,"position":3},"title":"#415 tight_layout() in matplotlib in python","author":"gantovnik","date":"2024-02-20","format":false,"excerpt":"[code language=\"python\"] 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=\".\", color=\"green\") ax3.hist(yy, bins=20, color=\"red\",edgecolor='black', alpha=.5) fig.tight_layout() plt.savefig(\"ex415.png\", dpi=100) plt.show() [\/code]","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\/2024\/02\/ex415.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex415.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex415.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex415.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1877,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/355-animation-of-domain-coloring-using-python\/","url_meta":{"origin":1188,"position":4},"title":"#355 Animation of domain coloring using python","author":"gantovnik","date":"2023-07-01","format":false,"excerpt":"[code language=\"python\"] # pip install celluloid #%matplotlib qt import numpy as np from matplotlib import pyplot as plt from matplotlib.colors import hsv_to_rgb from celluloid import Camera fig = plt.figure() camera = Camera(fig) for a in np.linspace(0, 2 * np.pi, 30, endpoint=False): x = np.linspace(-3, 3, 800) X, Y = np.meshgrid(x,\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\/ex355.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/07\/ex355.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/07\/ex355.gif?resize=525%2C300&ssl=1 1.5x"},"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":1188,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1188","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=1188"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1188\/revisions"}],"predecessor-version":[{"id":2478,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1188\/revisions\/2478"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}