Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Color a Scatter Plot by Category using Matplotlib in Python

#1
How to Color a Scatter Plot by Category using Matplotlib in Python

<div><h2>Problem Formulation</h2>
<p>Given three arrays:</p>
<ul>
<li>The first two arrays <code>x</code> and <code>y</code> of length <code>n</code> contain the <code>(x_i, y_i)</code> data of a 2D coordinate system.</li>
<li>The third array <code>c</code> provides categorical label information so we essentially get <code>n</code> data bundles <code>(x_i, y_i, c_i)</code> for an arbitrary number of categories <code>c_i</code>. </li>
</ul>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How to plot the data so that <code>(x_i, y_i)</code> and <code>(x_j, y_j)</code> with the same category <code>c_i == c_j</code> have the same color?</p>
<h2>Solution: Use Pandas groupby() and Call plt.plot() Separately for Each Group</h2>
<p class="has-global-color-8-background-color has-background">To plot data by category, you iterate over all groups separately by using the <code>data.groupby()</code> operation. For each group, you execute the <code>plt.plot()</code> operation to plot only the data in the group.</p>
<p>In particular, you perform the following steps:</p>
<ol>
<li>Use the <code>data.groupby("Category")</code> function assuming that data is a Pandas DataFrame containing the <code>x</code>, <code>y</code>, and<code> category</code> columns for <em>n</em> data points (rows). </li>
<li>Iterate over all <code>(name, group)</code> tuples in the grouping operation result obtained from step one.</li>
<li>Use <code>plt.plot(group["X"], group["Y"], marker="o", linestyle="", label=name)</code> to plot each group separately using the <code>x</code>, <code>y</code> data and <code>name</code> as a label.</li>
</ol>
<p>Here’s what that looks like in code:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="13-15" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd
import matplotlib.pyplot as plt # Generate the categorical data
x = [1, 2, 3, 4, 5, 6]
y = [42, 41, 40, 39, 38, 37]
c = ['a', 'b', 'a', 'b', 'b', 'a'] data = pd.DataFrame({"X": x, "Y": y, "Category": c})
print(data) # Plot data by category
groups = data.groupby("Category")
for name, group in groups: plt.plot(group["X"], group["Y"], marker="o", linestyle="", label=name) plt.legend()
plt.show()</pre>
<p>Before I show you how the resulting plot looks, allow me to show you the data output from the <code><a href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank" rel="noreferrer noopener">print()</a></code> function. Here’s the output of the categorical data:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""> X Y Category
0 1 42 a
1 2 41 b
2 3 40 a
3 4 39 b
4 5 38 b
5 6 37 a</pre>
<p>Now, how does the colored category plot look like? Here’s how:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="727" height="540" src="https://blog.finxter.com/wp-content/uploads/2022/04/image-61.png" alt="" class="wp-image-292400" srcset="https://blog.finxter.com/wp-content/uploads/2022/04/image-61.png 727w, https://blog.finxter.com/wp-content/uplo...00x223.png 300w" sizes="(max-width: 727px) 100vw, 727px" /></figure>
</div>
<p>If you want to learn more about Matplotlib, feel free to check out our full blog tutorial series:</p>
<ul>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/matplotlib-full-guide/" data-type="URL" data-id="https://blog.finxter.com/matplotlib-full-guide/" target="_blank">Python Matplotlib Full Guide</a></li>
<li><a href="https://blog.finxter.com/best-matplotlib-cheat-sheet/" data-type="post" data-id="22273" target="_blank" rel="noreferrer noopener">Matplotlib Cheat Sheets</a></li>
</ul>
</div>


https://www.sickgaming.net/blog/2022/04/...in-python/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016