<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Seaborn on 春江暮客</title>
    <link>https://www.bobobk.com/en/tags/seaborn/</link>
    <description>Recent content in Seaborn on 春江暮客</description>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Mon, 20 Dec 2021 08:19:34 +0800</lastBuildDate>
    <atom:link href="https://www.bobobk.com/en/tags/seaborn/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>K-Means Clustering and Implementation with sklearn</title>
      <link>https://www.bobobk.com/en/902.html</link>
      <pubDate>Mon, 20 Dec 2021 08:19:34 +0800</pubDate>
      <guid>https://www.bobobk.com/en/902.html</guid>
      <description>Cluster analysis allows us to find groups of similar samples or features, with stronger correlations among these objects. Common uses include grouping samples by different gene expression profiles or grouping genes by classifications of different samples.</description>
    </item>
    <item>
      <title>Detailed Examples of Seaborn Plotting Kernel Density Curves</title>
      <link>https://www.bobobk.com/en/263.html</link>
      <pubDate>Sun, 20 Jan 2019 11:41:58 +0000</pubDate>
      <guid>https://www.bobobk.com/en/263.html</guid>
      <description>&lt;p&gt;In a frequency distribution histogram, when the sample size is sufficiently enlarged to its limit, and the bin width is infinitely shortened, the step-like broken line in the frequency histogram will evolve into a smooth curve. This curve is called the density distribution curve of the population.&lt;/p&gt;&#xA;&lt;p&gt;In this article, Chunjing Muke will detail how to use the Python plotting library Seaborn and the Iris flower dataset from Pandas to plot various cool density curves.&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;1-basic-density-curve&#34;&gt;1. Basic Density Curve&lt;/h2&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; seaborn &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; sns&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set(color_codes&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_style(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pd&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;read_csv(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;iris.csv&amp;#39;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;kdeplot(df[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;sepal_width&amp;#39;&lt;/span&gt;])&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;img src=&#34;https://www.bobobk.com/wp-content/uploads/2019/01/basic_kdeplot.webp&#34; alt=&#34;‘Detailed Examples of Seaborn Plotting Kernel Density Curves’&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;To plot a kernel density curve using Seaborn, you only need to use &lt;code&gt;kdeplot&lt;/code&gt;. Note that a density curve only requires one variable; here we choose the &lt;code&gt;sepal_width&lt;/code&gt; column.&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;2-density-curve-with-shading&#34;&gt;2. Density Curve with Shading&lt;/h2&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; seaborn &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; sns&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set(color_codes&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_style(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pd&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;read_csv(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;iris.csv&amp;#39;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;kdeplot(df[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;sepal_width&amp;#39;&lt;/span&gt;],shade&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;img src=&#34;https://www.bobobk.com/wp-content/uploads/2019/01/shadow_kdeplot.webp&#34; alt=&#34;‘Detailed Examples of Seaborn Plotting Kernel Density Curves’&#34;&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>TypeError: ufunc &#39;isnan&#39; not supported for the input types - Solution</title>
      <link>https://www.bobobk.com/en/245.html</link>
      <pubDate>Mon, 14 Jan 2019 12:42:00 +0000</pubDate>
      <guid>https://www.bobobk.com/en/245.html</guid>
      <description>&lt;p&gt;Today, while using Python&amp;rsquo;s &lt;strong&gt;Seaborn&lt;/strong&gt; to plot a heatmap (clustermap), I kept encountering this error. My data seemed perfectly fine, and a Google search didn&amp;rsquo;t yield any good solutions. After some exploration, I&amp;rsquo;m sharing the final solution here.&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;1-generating-the-dataframe&#34;&gt;1. Generating the DataFrame&lt;/h2&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; pandas &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; pd&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; numpy &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; np&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; matplotlib.pyplot &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; plt&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; seaborn &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; clustermap&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; seaborn &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; sns; sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set(color_codes&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pd&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;DataFrame([[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;a&amp;#34;&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;b&amp;#34;&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;c&amp;#34;&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;d&amp;#34;&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;e&amp;#34;&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;f&amp;#34;&lt;/span&gt;],[&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt;],[&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;7&lt;/span&gt;],[&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;7&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;8&lt;/span&gt;]],  columns&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;list(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;ABCDEF&amp;#39;&lt;/span&gt;))&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;T&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    df&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    g &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;clustermap(df&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[:,&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;:],cmap&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;PiYG&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;After generating and transposing the &lt;strong&gt;DataFrame&lt;/strong&gt;, a &lt;code&gt;TypeError&lt;/code&gt; occurs: &lt;code&gt;TypeError: ufunc &#39;isnan&#39; not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule &amp;quot;safe&amp;quot;&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://www.bobobk.com/wp-content/uploads/2019/01/isnan_error.webp&#34; alt=&#34;《TypeError: ufunc ‘isnan’ not supported for the input types solution》&#34;&gt;&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;2-cause-of-the-error&#34;&gt;2. Cause of the Error&lt;/h2&gt;&#xA;&lt;p&gt;This type of error arises because the &lt;strong&gt;DataFrame&lt;/strong&gt; has been transposed, and the original &lt;strong&gt;DataFrame&lt;/strong&gt; contained string columns. Just like in the example above, the first column contains strings (values &amp;lsquo;abcdef&amp;rsquo;). When transposed, all numerical values in the &lt;strong&gt;DataFrame&lt;/strong&gt; are also converted to &lt;code&gt;object&lt;/code&gt; types instead of &lt;code&gt;float&lt;/code&gt; or &lt;code&gt;int&lt;/code&gt; numerical types. Therefore, trying to plot a heatmap with character types naturally leads to an error.&lt;/p&gt;</description>
    </item>
    <item>
      <title>TypeError: ufunc &#39;isnan&#39; not supported for the input types - Solution</title>
      <link>https://www.bobobk.com/en/245.html</link>
      <pubDate>Mon, 14 Jan 2019 12:42:00 +0000</pubDate>
      <guid>https://www.bobobk.com/en/245.html</guid>
      <description>&lt;p&gt;After generating and transposing the DataFrame, a TypeError occurred: TypeError: ufunc &amp;lsquo;isnan&amp;rsquo; not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule &amp;ldquo;safe&amp;rdquo;.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://www.bobobk.com/wp-content/uploads/2019/01/isnan_error.webp&#34; alt=&#34;《TypeError: ufunc ‘isnan’ not supported for the input types - Solution》&#34;&gt;&lt;/p&gt;&#xA;&lt;h2 id=&#34;2-cause-of-the-error&#34;&gt;2. Cause of the Error&lt;/h2&gt;&#xA;&lt;p&gt;This type of error occurs because the DataFrame has been transposed, and the original DataFrame contains a column with strings. Just like in the example above, the first column contains string values &amp;ldquo;abcdef&amp;rdquo;. After transposition, all numbers in the DataFrame also become &amp;ldquo;object&amp;rdquo; type instead of &amp;ldquo;float&amp;rdquo; or &amp;ldquo;int&amp;rdquo; numeric types. Therefore, when we try to plot a heatmap with character types, an error naturally occurs.&lt;/p&gt;&#xA;&lt;p&gt;If the DataFrame originally contained only numeric types, there would be no issue here.&lt;/p&gt;&#xA;&lt;h2 id=&#34;3-solution&#34;&gt;3. Solution&lt;/h2&gt;&#xA;&lt;p&gt;Knowing the cause, the solution is simple: convert the corresponding numeric columns in the transposed DataFrame to numeric types. Here&amp;rsquo;s the code:&lt;/p&gt;</description>
    </item>
    <item>
      <title>Drawing Violin Plots with Seaborn</title>
      <link>https://www.bobobk.com/en/206.html</link>
      <pubDate>Fri, 04 Jan 2019 12:02:11 +0000</pubDate>
      <guid>https://www.bobobk.com/en/206.html</guid>
      <description>&lt;h2 id=&#34;introduction&#34;&gt;Introduction&lt;/h2&gt;&#xA;&lt;p&gt;A violin plot is used to display the distribution and probability density of multiple data groups. Similar to a box plot, it offers a better representation of data density. Violin plots are particularly useful when dealing with very large datasets that are difficult to display individually. Python&amp;rsquo;s Seaborn package makes it very convenient to create violin plots.&lt;/p&gt;&#xA;&lt;h2 id=&#34;parameters&#34;&gt;Parameters&lt;/h2&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://www.bobobk.com/wp-content/uploads/2019/01/violin.webp&#34; alt=&#34;Drawing Violin Plots with Seaborn&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;The parameters corresponding to each position in a violin plot are shown above. The middle line represents the box plot data, specifically the 25th, 50th (median), and 75th percentiles. The thin lines indicate the 95% confidence interval.&lt;/p&gt;&#xA;&lt;h2 id=&#34;drawing-violin-plots-with-seaborn&#34;&gt;Drawing Violin Plots with Seaborn&lt;/h2&gt;&#xA;&lt;h3 id=&#34;single-variable-data&#34;&gt;Single Variable Data&lt;/h3&gt;&#xA;&lt;p&gt;While a box plot would suffice for a single variable, a violin plot can certainly be used as well:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; seaborn &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; sns&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set(color_codes&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_style(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;load_dataset(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;iris&amp;#39;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;violinplot( y&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;df[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;sepal_length&amp;#34;&lt;/span&gt;] )&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;img src=&#34;https://www.bobobk.com/wp-content/uploads/2019/01/oned.webp&#34; alt=&#34;Drawing Violin Plots with Seaborn&#34;&gt;&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
