{"id":1977,"date":"2023-11-16T22:20:36","date_gmt":"2023-11-17T06:20:36","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1977"},"modified":"2023-11-16T22:20:36","modified_gmt":"2023-11-17T06:20:36","slug":"396-seaborn-pairplot-example-with-lowess-regression","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2023\/11\/396-seaborn-pairplot-example-with-lowess-regression\/","title":{"rendered":"#396 Seaborn pairplot example with lowess regression"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport matplotlib.pyplot as plt\r\nimport seaborn as sns\r\n\r\ndef plot_extra(x, y, **kwargs):\r\n     if kwargs&#x5B;'label'] == first_label:\r\n          sns.regplot(data=kwargs&#x5B;'data'], x=x.name, y=y.name, lowess=True, scatter=False, color=kwargs&#x5B;'color'])\r\n\r\ndf = sns.load_dataset('iris')\r\nfirst_label = df&#x5B;'species']&#x5B;0]\r\npg = sns.pairplot(df, hue='species', plot_kws={'alpha': 0.8}, palette='rainbow')\r\npg.map_offdiag(plot_extra, color='crimson', data=df)\r\nlegend_dict = {h.get_label(): h for h in pg.legend.legend_handles}  # the existing legend items\r\nlegend_dict&#x5B;'lowess regression'] = pg.axes&#x5B;0, 1].lines&#x5B;0]\r\n# add the first line object of a non-diagonal ax to the legend\r\npg.legend.remove()  # remove existing legend\r\npg.add_legend(legend_dict, label_order=legend_dict.keys(), title='')  # create the new legend\r\nplt.savefig(&quot;ex396.png&quot;, dpi=100)\r\nplt.show()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import matplotlib.pyplot as plt import seaborn as sns def plot_extra(x, y, **kwargs): if kwargs&#x5B;&#8217;label&#8217;] == first_label: sns.regplot(data=kwargs&#x5B;&#8217;data&#8217;], x=x.name, y=y.name, lowess=True, scatter=False, color=kwargs&#x5B;&#8217;color&#8217;]) df = sns.load_dataset(&#8216;iris&#8217;) first_label = df&#x5B;&#8217;species&#8217;]&#x5B;0] pg = sns.pairplot(df, hue=&#8217;species&#8217;, plot_kws={&#8216;alpha&#8217;: 0.8}, palette=&#8217;rainbow&#8217;) pg.map_offdiag(plot_extra, color=&#8217;crimson&#8217;, data=df) legend_dict = {h.get_label(): h for h in pg.legend.legend_handles} # the existing legend items legend_dict&#x5B;&#8217;lowess regression&#8217;] = pg.axes&#x5B;0, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1978,"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-1977","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/11\/ex396.png?fit=1290%2C1000&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-vT","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1032,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/184-creating-a-boxplot-using-seaborn\/","url_meta":{"origin":1977,"position":0},"title":"#183 Creating a boxplot using seaborn","author":"gantovnik","date":"2021-11-07","format":false,"excerpt":"[code language=\"python\"] import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import os os.chdir(r'D:\\projects\\wordpress\\ex184') os.getcwd() url = \"https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/auto-mpg\/auto-mpg.data\" df = pd.read_csv(url) column_names = ['mpg', 'cylinders', 'displacement', 'horsepower', 'weight', 'acceleration', 'year', 'origin', 'name'] df = pd.read_csv(url, names= column_names, delim_whitespace=True) print(df.head()) #Convert the horsepower and\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\/ex184.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex184.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex184.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex184.jpg?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex184.jpg?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex184.jpg?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":1037,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/185-creating-a-scatter-plot-using-seaborn\/","url_meta":{"origin":1977,"position":1},"title":"#185 Creating a scatter plot using seaborn","author":"gantovnik","date":"2021-11-07","format":false,"excerpt":"[code language=\"python\"] import matplotlib.pyplot as plt import numpy as np import pandas as pd import os import seaborn as sns os.chdir(r'D:\\projects\\wordpress\\ex185') os.getcwd() url = \"https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/auto-mpg\/auto-mpg.data\" df = pd.read_csv(url) column_names = ['mpg', 'cylinders', 'displacement', 'horsepower', 'weight', 'acceleration', 'year', 'origin', 'name'] df = pd.read_csv(url, names= column_names, delim_whitespace=True) df.loc[df.horsepower == '?', 'horsepower'] =\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\/ex185b.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex185b.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex185b.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex185b.jpg?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex185b.jpg?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex185b.jpg?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":1943,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/08\/385-scatterplot-matrix-using-seaborn-library\/","url_meta":{"origin":1977,"position":2},"title":"#385 Scatterplot matrix using seaborn library","author":"gantovnik","date":"2023-08-13","format":false,"excerpt":"[code language=\"python\"] #Scatterplot matrix using seaborn library import matplotlib.pyplot as plt #conda install -c anaconda seaborn import seaborn as sns sns.set_theme(style=\"ticks\") def main(): df = sns.load_dataset(\"penguins\") sns.pairplot(df, hue=\"species\") plt.savefig(\"ex385.png\", dpi=100) plt.show() if __name__ == '__main__': main() [\/code]","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\/ex385.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/08\/ex385.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/08\/ex385.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/08\/ex385.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/08\/ex385.png?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":505,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/05\/creating-a-better-version-of-python-3-tkinter-hello-world\/","url_meta":{"origin":1977,"position":3},"title":"#75 Creating a better version of Python 3 Tkinter Hello World","author":"gantovnik","date":"2020-05-06","format":false,"excerpt":"Creating a better version of Python 3 Tkinter Hello World [code language=\"python\"] import tkinter as tk from tkinter import ttk class HelloView(tk.Frame): \"\"\"A friendly little module\"\"\" def __init__(self, parent, *args, **kwargs): super().__init__(parent, *args, **kwargs) self.name = tk.StringVar() self.hello_string = tk.StringVar() self.hello_string.set(\"Hello World\") name_label = ttk.Label(self, text=\"Name:\") name_entry = ttk.Entry(self, textvariable=self.name)\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\/2020\/05\/2020-05-06_000413.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1232,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":1977,"position":4},"title":"#220 Custom figure subclasses","author":"gantovnik","date":"2021-11-28","format":false,"excerpt":"[code language=\"python\"] import matplotlib.pyplot as plt from matplotlib.figure import Figure import numpy as np class WatermarkFigure(Figure): \"\"\"A figure with a text watermark.\"\"\" def __init__(self, *args, watermark=None, **kwargs): super().__init__(*args, **kwargs) if watermark is not None: bbox = dict(boxstyle='square', lw=3, ec='gray', fc=(0.9, 0.9, .9, .5), alpha=0.5) self.text(0.5, 0.5, watermark, ha='center', va='center', rotation=30,\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\/ex220.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1028,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/183-creating-a-histogram-using-seaborn\/","url_meta":{"origin":1977,"position":5},"title":"#184 Creating a histogram using seaborn","author":"gantovnik","date":"2021-11-07","format":false,"excerpt":"[code language=\"python\"] import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import os os.chdir(r'D:\\projects\\wordpress\\ex183') os.getcwd() url = \"https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/auto-mpg\/auto-mpg.data\" df = pd.read_csv(url) column_names = ['mpg', 'cylinders', 'displacement', 'horsepower', 'weight', 'acceleration', 'year', 'origin', 'name'] df = pd.read_csv(url, names= column_names, delim_whitespace=True) print(df.head()) #Convert the horsepower and\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\/ex183.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex183.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex183.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex183.jpg?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex183.jpg?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex183.jpg?resize=1400%2C800&ssl=1 4x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1977","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=1977"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1977\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media\/1978"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}