<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Data for Everybody</title>
	<atom:link href="https://www.dataforeverybody.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dataforeverybody.com</link>
	<description>Beginning Data Analysis and Visualization</description>
	<lastBuildDate>Sun, 08 Mar 2026 17:44:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.dataforeverybody.com/wp-content/uploads/2026/03/cropped-logo-512-32x32.png</url>
	<title>Data for Everybody</title>
	<link>https://www.dataforeverybody.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Plot horizontal lines in Matplotlib and Seaborn with Python</title>
		<link>https://www.dataforeverybody.com/matplotlib-python-plot-horizontal-line/</link>
					<comments>https://www.dataforeverybody.com/matplotlib-python-plot-horizontal-line/#respond</comments>
		
		<dc:creator><![CDATA[Gili]]></dc:creator>
		<pubDate>Tue, 24 Aug 2021 08:04:04 +0000</pubDate>
				<category><![CDATA[Data Visualization]]></category>
		<guid isPermaLink="false">https://www.dataforeverybody.com/?p=720</guid>

					<description><![CDATA[<p>In today&#8217;s quick Data visualization tutorial, we will show you how you can easily draw horizontal lines in Python plots that run in parallel to ... </p>
<p class="read-more-container"><a title="Plot horizontal lines in Matplotlib and Seaborn with Python" class="read-more button" href="https://www.dataforeverybody.com/matplotlib-python-plot-horizontal-line/#more-720" aria-label="More on Plot horizontal lines in Matplotlib and Seaborn with Python">Read more</a></p>
<p>The post <a href="https://www.dataforeverybody.com/matplotlib-python-plot-horizontal-line/">Plot horizontal lines in Matplotlib and Seaborn with Python</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>In today&#8217;s quick Data visualization tutorial, we will show you how you can easily draw horizontal lines in Python plots that run in parallel to the X axis. We&#8217;ll use a bar plot as an example, but you can obviously apply the same logic for other charts such as scatter, histogram, lien plots etc&#8217;. Once we render the line over our chart, we&#8217;ll demonstrate some simple customization options.</p>
<h3 class="wp-block-heading">Preparation</h3>
<p>We&#8217;ll start by defining some dummy data for our chart by leveraging the numpy library.</p>
<pre class="wp-block-code"><code>import numpy as np
import matplotlib.pyplot as plt

np.random.seed(10)

x = &#91;'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday']
y = np.random.randint(75,100,5)</code></pre>
<h3 class="wp-block-heading">Python horizontal line plotting using Matplotlib</h3>
<p>We&#8217;ll first use Matplotlib to render our plot:</p>
<pre class="wp-block-code"><code>fig, ax= plt.subplots(figsize=(9,5))
ax.bar(x, height=y);

# Now we'll use the plt.axhline method to render the horizontal line at y=81

fig, ax= plt.subplots(figsize=(9,5))

ax.bar(x, height=y);
ax.axhline(y=81);
</code></pre>
<p>Here&#8217;s the result:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="537" height="278" src="https://www.dataforeverybody.com/wp-content/uploads/2021/08/python_plot_horizontal_grid.png" alt="img[python_plot_horizontal_grid.png] alt='Python Matplotlib horizontal grid lines example" class="wp-image-723" srcset="https://www.dataforeverybody.com/wp-content/uploads/2021/08/python_plot_horizontal_grid.png 537w, https://www.dataforeverybody.com/wp-content/uploads/2021/08/python_plot_horizontal_grid-300x155.png 300w" sizes="auto, (max-width: 537px) 100vw, 537px" /></figure>
</div>
<h3 class="wp-block-heading">Customizing the Matplotlib axhline example</h3>
<p>We&#8217;ll customize the line style by defining custom color, marker style, line style and width as shown below:</p>
<pre class="wp-block-code"><code>fig, ax= plt.subplots(figsize=(9,5))

ax.bar(x, height=y);
ax.axhline(y=81, <strong>color='green', marker='o', linestyle='--', linewidth = 5</strong>);</code></pre>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="518" height="278" src="https://www.dataforeverybody.com/wp-content/uploads/2021/08/matplotlib_axhline_example.png" alt="img[matplotlib_axhline_example.png] alt='Matplotlib axhline horizontal line plot example" class="wp-image-724" srcset="https://www.dataforeverybody.com/wp-content/uploads/2021/08/matplotlib_axhline_example.png 518w, https://www.dataforeverybody.com/wp-content/uploads/2021/08/matplotlib_axhline_example-300x161.png 300w" sizes="auto, (max-width: 518px) 100vw, 518px" /></figure>
</div>
<h3 class="wp-block-heading">Plotting multiple lines</h3>
<p> To add multiple lines to the chart, we&#8217;ll simply invoke axhline as needed.</p>
<pre class="wp-block-code"><code>fig, ax= plt.subplots(figsize=(9,5))

ax.bar(x, height=y);
ax.axhline(y=81, color='green', marker='o', linestyle='--', linewidth = 5);
<strong>ax.axhline(y=61, color='red', marker='o', linestyle='--', linewidth = 5);</strong></code></pre>
<p>Here&#8217;s our chart:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="559" height="319" src="https://www.dataforeverybody.com/wp-content/uploads/2021/08/plot_matplotlib_multiple_lines_example.png" alt="img[plot_matplotlib_multiple_lines_example.png] alt='Multiple horizontal lines plotted in Matplotlib" class="wp-image-725" srcset="https://www.dataforeverybody.com/wp-content/uploads/2021/08/plot_matplotlib_multiple_lines_example.png 559w, https://www.dataforeverybody.com/wp-content/uploads/2021/08/plot_matplotlib_multiple_lines_example-300x171.png 300w" sizes="auto, (max-width: 559px) 100vw, 559px" /></figure>
</div>
<h3 class="wp-block-heading">Drawing grid lines</h3>
<p>If we want to easily draw gridlines in the background of our chart we can call plt.grid method</p>
<pre class="wp-block-code"><code>ax.grid()</code></pre>
<p>And here&#8217;s our chart</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="569" height="314" src="https://www.dataforeverybody.com/wp-content/uploads/2021/08/grid_lines_matplotlib_plot.png" alt="img[grid_lines_matplotlib_plot.png] alt='Custom grid lines in Matplotlib chart" class="wp-image-726" srcset="https://www.dataforeverybody.com/wp-content/uploads/2021/08/grid_lines_matplotlib_plot.png 569w, https://www.dataforeverybody.com/wp-content/uploads/2021/08/grid_lines_matplotlib_plot-300x166.png 300w" sizes="auto, (max-width: 569px) 100vw, 569px" /></figure>
</div>
<p>Note: if you prefer the grid line to be displayed behind the chart, just use the zorder parameter.</p>
<p>Here&#8217;s the example:</p>
<pre class="wp-block-code"><code>fig, ax= plt.subplots(figsize=(9,5))

ax.bar(x, height=y, <strong>zorder=10</strong>);
ax.grid(<strong>zorder=0</strong>)</code></pre>
<p>Vertical lines plotting will be covered in our next tutorial.</p><p>The post <a href="https://www.dataforeverybody.com/matplotlib-python-plot-horizontal-line/">Plot horizontal lines in Matplotlib and Seaborn with Python</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.dataforeverybody.com/matplotlib-python-plot-horizontal-line/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Create a Seaborn countplot using Python: a step by step example</title>
		<link>https://www.dataforeverybody.com/countplot-seaborn-order-size-values/</link>
					<comments>https://www.dataforeverybody.com/countplot-seaborn-order-size-values/#respond</comments>
		
		<dc:creator><![CDATA[Gili]]></dc:creator>
		<pubDate>Wed, 30 Dec 2020 19:49:46 +0000</pubDate>
				<category><![CDATA[Data Visualization]]></category>
		<guid isPermaLink="false">https://www.dataforeverybody.com/?p=642</guid>

					<description><![CDATA[<p>In today&#8217;s tutorial we would like to run through a detailed end to end example of Seaborn countplots creation and customization. We&#8217;ll be using our ... </p>
<p class="read-more-container"><a title="Create a Seaborn countplot using Python: a step by step example" class="read-more button" href="https://www.dataforeverybody.com/countplot-seaborn-order-size-values/#more-642" aria-label="More on Create a Seaborn countplot using Python: a step by step example">Read more</a></p>
<p>The post <a href="https://www.dataforeverybody.com/countplot-seaborn-order-size-values/">Create a Seaborn countplot using Python: a step by step example</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>In today&#8217;s tutorial we would like to run through a detailed end to end example of Seaborn countplots creation and customization. We&#8217;ll be using our deliveries DataFrame as an example. You can grab the file from <a href="https://www.dataforeverybody.com/wp-content/uploads/2020/11/deliveries.csv" target="_blank" rel="noreferrer noopener" title="https://www.dataforeverybody.com/wp-content/uploads/2020/11/deliveries.csv">this location</a>.</p>
<h2 class="wp-block-heading">Step 1: Import thew example dataset</h2>
<pre class="wp-block-code"><code>#Python3

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
sns.set_style('whitegrid')

# Read the dataset into a Pandas DataFrame
deliveries = pd.read_csv('../../data/deliveries.csv')</code></pre>
<h2 class="wp-block-heading">Step 2:  Create a Seaborn Countplot chart</h2>
<p>We&#8217;ll start by rendering the countplot:</p>
<pre class="wp-block-code"><code># define the figure and plot; modify the countplot figure size
countplt, ax = plt.subplots(figsize = (10,7))
ax =sns.countplot(x = 'day', data=deliveries)</code></pre>
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="618" height="426" src="https://www.dataforeverybody.com/wp-content/uploads/2020/12/seaborn_countplot.png" alt="Seaborn countplot basic example" class="wp-image-648" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/12/seaborn_countplot.png 618w, https://www.dataforeverybody.com/wp-content/uploads/2020/12/seaborn_countplot-300x207.png 300w" sizes="auto, (max-width: 618px) 100vw, 618px" /></figure>
<p>Note: Use the figsize parameter to control the countplot proportions, width and height of our figure. Here&#8217;s a more detailed post and information on <a href="https://www.dataforeverybody.com/seaborn-plot-figure-size/">customizing Seaborn plot figsize</a>.</p>
<h2 class="wp-block-heading">Step 2: Associate a custom Seaborn palette</h2>
<p>We can easily associate a predefined Seaborn palette to our plot. </p>
<pre class="wp-block-code"><code># set countplot palette
ax =sns.countplot(x = 'day', data=deliveries, order = day_order, palette='pastel' )</code></pre>
<h2 class="wp-block-heading">Step 3: Add titles to the plot and axes</h2>
<p>Our chart doesn&#8217;t make much sense without titles. We&#8217;ll use the plt.set_title and plt.set_xlabel methods to add titles to our plot.</p>
<pre class="wp-block-code"><code># Chart and axes titles
ax.set_title('Daily Deliveries',fontsize = 18, fontweight='bold' )
ax.set_xlabel('Day', fontsize = 15)
ax.set_ylabel('Delivery count', fontsize = 15)
countplt</code></pre>
<h2 class="wp-block-heading">Step 4: order of x-axis labels in Countplots</h2>
<p>As you can see above, the current order of the x-axis ticks doesn&#8217;t make much sense. Let&#8217;s modify that.</p>
<pre class="wp-block-code"><code># change order x axis + change pallete
day_order = &#91;'SUN','MON','TUE','WED','THU','FRI','SAT']
countplt, ax = plt.subplots(figsize = (10,7))
ax =sns.countplot(x = 'day', data=deliveries, order = day_order, palette='pastel' )</code></pre>
<p>Let&#8217;s take a look at our chart.</p>
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="637" height="455" src="https://www.dataforeverybody.com/wp-content/uploads/2020/12/countplot_title_order.png" alt="Countplot with custom color palette" class="wp-image-649" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/12/countplot_title_order.png 637w, https://www.dataforeverybody.com/wp-content/uploads/2020/12/countplot_title_order-300x214.png 300w" sizes="auto, (max-width: 637px) 100vw, 637px" /></figure>
<h2 class="wp-block-heading">Step 5: Show count in Seaborn countplots</h2>
<p>A nice addition to our chart would be the ability to show the value count for every bar.</p>
<p>Here&#8217;s a simple code that uses plt.text() to annotate the count values on top of our plot.</p>
<pre class="wp-block-code"><code># show count (+ annotate)
for rect in ax.patches:
    ax.text (rect.get_x() + rect.get_width()  / 2,rect.get_height()+ 0.75,rect.get_height(),horizontalalignment='center', fontsize = 11)
countplt</code></pre>
<p>Looking good:</p>
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="647" height="455" src="https://www.dataforeverybody.com/wp-content/uploads/2020/12/seaborn_countplot_show_value_count.png" alt="Seaborn countplot with ordered x-axis labels" class="wp-image-650" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/12/seaborn_countplot_show_value_count.png 647w, https://www.dataforeverybody.com/wp-content/uploads/2020/12/seaborn_countplot_show_value_count-300x211.png 300w" sizes="auto, (max-width: 647px) 100vw, 647px" /></figure>
<h2 class="wp-block-heading">Step 6: Multiple categorical columns in sns countplot</h2>
<p>We now want to show the usage of the <strong><em>hue</em></strong> parameter of sns.countplot() to achieve a categorical drill down of our delivery data.</p>
<pre class="wp-block-code"><code># categorical countplot - show multiple columns
countplt, ax = plt.subplots(figsize = (10,7))
ax =sns.countplot(x = 'day', data=deliveries, order = day_order, palette='pastel', hue='time')
ax.set_title('Daily Deliveries',fontsize = 18, fontweight='bold' )
ax.set_xlabel('Day', fontsize = 15)
ax.set_ylabel('Delivery count', fontsize = 15)
for rect in ax.patches:
    ax.text (rect.get_x() + rect.get_width()  / 2,rect.get_height()+ 0.25,rect.get_height(),horizontalalignment='center', fontsize = 9)
</code></pre>
<p>Let&#8217;s take a look:</p>
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="639" height="445" src="https://www.dataforeverybody.com/wp-content/uploads/2020/12/countplot_show_multiple_valuet.png" alt="Countplot showing count values on bars" class="wp-image-655" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/12/countplot_show_multiple_valuet.png 639w, https://www.dataforeverybody.com/wp-content/uploads/2020/12/countplot_show_multiple_valuet-300x209.png 300w" sizes="auto, (max-width: 639px) 100vw, 639px" /></figure>
<h2 class="wp-block-heading"> Step 7: Add the legend to the countplot</h2>
<p>As can be seen above, the plot legend is overlapping with the top right of the chart. We would like to place the legend outside the countplot. We&#8217;ll use the bbox_to_anchor parameter to define a bounding box for the chart legend.</p>
<pre class="wp-block-code"><code>#countplot legend outside the chart
ax.legend(fontsize = 12, \
               bbox_to_anchor= (1.01, 1), \
               title="Time", \
               title_fontsize = 15);
countplt</code></pre>
<p>Here&#8217;s the result:</p>
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="723" height="460" src="https://www.dataforeverybody.com/wp-content/uploads/2020/12/seaborn_countplot_legend_outside.png" alt="Multiple category Seaborn countplot" class="wp-image-657" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/12/seaborn_countplot_legend_outside.png 723w, https://www.dataforeverybody.com/wp-content/uploads/2020/12/seaborn_countplot_legend_outside-300x191.png 300w" sizes="auto, (max-width: 723px) 100vw, 723px" /></figure>
<p>Note: For more information about chart legend customization, check out our comprehensive <a href="https://www.dataforeverybody.com/seaborn-legend-change-location-size/" target="_blank" rel="noreferrer noopener" title="How to change Seaborn legends font size, location and color?">tutorial on Seaborn legends</a>.</p>
<h2 class="wp-block-heading">Further learning:</h2>
<ul>
<li><a href="https://www.dataforeverybody.com/seaborn-set-axes-labels/" target="_blank" rel="noreferrer noopener" title="How to set axes labels &amp; limits in a Seaborn plot?">How to set axes labels &amp; limits in a Seaborn plot?</a></li>
</ul><p>The post <a href="https://www.dataforeverybody.com/countplot-seaborn-order-size-values/">Create a Seaborn countplot using Python: a step by step example</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.dataforeverybody.com/countplot-seaborn-order-size-values/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to customize Matplotlib plot titles color, position and fonts?</title>
		<link>https://www.dataforeverybody.com/matplotlib-title-size-position-color/</link>
					<comments>https://www.dataforeverybody.com/matplotlib-title-size-position-color/#respond</comments>
		
		<dc:creator><![CDATA[Gili]]></dc:creator>
		<pubDate>Wed, 30 Dec 2020 16:58:07 +0000</pubDate>
				<category><![CDATA[Data Visualization]]></category>
		<guid isPermaLink="false">https://www.dataforeverybody.com/?p=632</guid>

					<description><![CDATA[<p>In today&#8217;s quick recipe, we&#8217;ll learn the basic of Matplotlib title customization. We&#8217;ll use bar plots, but the post is fully relevant for other Matplotlib ... </p>
<p class="read-more-container"><a title="How to customize Matplotlib plot titles color, position and fonts?" class="read-more button" href="https://www.dataforeverybody.com/matplotlib-title-size-position-color/#more-632" aria-label="More on How to customize Matplotlib plot titles color, position and fonts?">Read more</a></p>
<p>The post <a href="https://www.dataforeverybody.com/matplotlib-title-size-position-color/">How to customize Matplotlib plot titles color, position and fonts?</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>In today&#8217;s quick recipe, we&#8217;ll learn the basic of Matplotlib title customization. We&#8217;ll use bar plots, but the post is fully relevant for other Matplotlib charts: line, scatter, distribution and bar plots.</p>
<h2 class="wp-block-heading">Step 1: Prepare your data for visualization</h2>
<p>We&#8217;ll start by defining a simple dataset for this example.</p>
<pre class="wp-block-code"><code># Python3

# import pandas and matplotlib
import pandas as pd
import matplotlib.pyplot as plt

# Create Dataframe dataframe 
revenue = pd.DataFrame({"city":&#91;'Chicago', 'Boca Raton', 'Miami','Omaha']* 2,
                    "sales": &#91;3568, 2367, 2152, 3027, 3695, 2495, 4134, 2162]})
 
# Grouping the data 
rev_by_city = revenue.groupby('city')&#91;'sales'].sum()</code></pre>
<p>We got a Pandas Series that we&#8217;ll quickly visualize using Pandas &amp; Matplotlib</p>
<h2 class="wp-block-heading">Step 2: Draw your Matplotlib plot</h2>
<p>Let&#8217;s set a very simple plot using Matplotlib:</p>
<pre class="wp-block-code"><code>x = rev_by_city.index
y = rev_by_city.values

fig, ax = plt.subplots(dpi = 147)
ax.bar(x,y, color='green');</code></pre>
<p>We got a very simple chart.</p>
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="823" height="509" src="https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_plot.png" alt="img[matplotlib_plot.png] alt='Basic Matplotlib plot with default title" class="wp-image-634" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_plot.png 823w, https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_plot-300x186.png 300w, https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_plot-768x475.png 768w" sizes="auto, (max-width: 823px) 100vw, 823px" /></figure>
<p>Note: We could have got a somewhat similar output using the Pandas library only:</p>
<pre class="wp-block-code"><code>rev_by_city.plot(kind='bar', color='green');</code></pre>
<h2 class="wp-block-heading">Step 3: Matplotlib chart custom titles</h2>
<p>Let&#8217;s run the get_title() method on our plot:</p>
<pre class="wp-block-code"><code>ax.get_title()</code></pre>
<p>As expected, the result is an empty string.</p>
<h3 class="wp-block-heading">Define plot titles</h3>
<p>The plt.set_title() method is self explanatory. It allows to define a title for your chart.</p>
<pre class="wp-block-code"><code>ax.set_title('Sales by City');
fig</code></pre>
<h3 class="wp-block-heading">Customize Matplotlib title fonts size and color</h3>
<p>Let&#8217;s quickly define a title and customize the font size, weight and color.</p>
<pre class="wp-block-code"><code>ax.set_title('Sales by City', fontsize=15, color= 'blue', fontweight='bold');
fig</code></pre>
<p>Here&#8217;s the result:</p>
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="792" height="542" src="https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_titles_fonts_size_bold.png" alt="img[matplotlib_titles_fonts_size_bold.png] alt='Matplotlib title with custom font size and bold weight" class="wp-image-635" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_titles_fonts_size_bold.png 792w, https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_titles_fonts_size_bold-300x205.png 300w, https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_titles_fonts_size_bold-768x526.png 768w" sizes="auto, (max-width: 792px) 100vw, 792px" /></figure>
<h2 class="wp-block-heading">Set Matplotlib title position</h2>
<p>By default the title is aligned to the center of the plot. Using the loc parameter, you are able to  left and right align it.</p>
<p>In this case, we&#8217;ll left align the title:</p>
<pre class="wp-block-code"><code>fig, ax = plt.subplots(dpi = 147)
ax.bar(x,y, color='green');
ax.set_title('Sales by City', fontsize=15, color= 'blue', fontweight='bold',loc='left');</code></pre>
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="860" height="540" src="https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_titles_position.png" alt="img[matplotlib_titles_position.png] alt='Matplotlib title position customization example" class="wp-image-636" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_titles_position.png 860w, https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_titles_position-300x188.png 300w, https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_titles_position-768x482.png 768w" sizes="auto, (max-width: 860px) 100vw, 860px" /></figure>
<p>We can also use the y parameter to determine the margin from the title to the chart</p>
<pre class="wp-block-code"><code>fig, ax = plt.subplots(dpi = 147)
ax.bar(x,y, color='green');
ax.set_title('Sales by City', fontsize=15, color= 'blue', fontweight='bold', y= 1.1);</code></pre>
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="862" height="595" src="https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_plot_margin_position.png" alt="img[matplotlib_plot_margin_position.png] alt='Matplotlib title with custom margin positioning" class="wp-image-637" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_plot_margin_position.png 862w, https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_plot_margin_position-300x207.png 300w, https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_plot_margin_position-768x530.png 768w" sizes="auto, (max-width: 862px) 100vw, 862px" /></figure>
<h2 class="wp-block-heading">Matplotlib title inside plot</h2>
<p>What if we want to place the title so it will overlap with the inside part of the plot figure? That&#8217;s possible by setting the y parameter to a negative figure, in our case -0.9. We also used the <strong>pad </strong> parameter to increase the padding from the figure border:</p>
<pre class="wp-block-code"><code>fig, ax = plt.subplots(dpi = 147)
ax.bar(x,y, color='green');
ax.set_title('Sales by City', fontsize=15, color= 'blue', fontweight='bold', loc='left', y=0.9,  pad= -10);</code></pre>
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="817" height="516" src="https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_titles_inside_chart.png" alt="img[matplotlib_titles_inside_chart.png] alt='Matplotlib title placed inside the chart area" class="wp-image-638" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_titles_inside_chart.png 817w, https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_titles_inside_chart-300x189.png 300w, https://www.dataforeverybody.com/wp-content/uploads/2020/12/matplotlib_titles_inside_chart-768x485.png 768w" sizes="auto, (max-width: 817px) 100vw, 817px" /></figure>
<p>If you are using Seaborn, you might want to look into a similar tutorial on how to <a href="https://www.dataforeverybody.com/change-seaborn-title-chart-axes-font/">customize Seaborn titles</a>.</p><p>The post <a href="https://www.dataforeverybody.com/matplotlib-title-size-position-color/">How to customize Matplotlib plot titles color, position and fonts?</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.dataforeverybody.com/matplotlib-title-size-position-color/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Pandas: split one column into two or multiple columns  in Python</title>
		<link>https://www.dataforeverybody.com/pandas-split-columns-dataframe/</link>
					<comments>https://www.dataforeverybody.com/pandas-split-columns-dataframe/#respond</comments>
		
		<dc:creator><![CDATA[Gili]]></dc:creator>
		<pubDate>Sun, 27 Dec 2020 18:22:58 +0000</pubDate>
				<category><![CDATA[Data Wrangling]]></category>
		<guid isPermaLink="false">https://www.dataforeverybody.com/?p=608</guid>

					<description><![CDATA[<p>In today&#8217;s quick tutorial we&#8217;ll learn how to re-format your column contents so that you can split data located in a DataFrame column into one ... </p>
<p class="read-more-container"><a title="Pandas: split one column into two or multiple columns  in Python" class="read-more button" href="https://www.dataforeverybody.com/pandas-split-columns-dataframe/#more-608" aria-label="More on Pandas: split one column into two or multiple columns  in Python">Read more</a></p>
<p>The post <a href="https://www.dataforeverybody.com/pandas-split-columns-dataframe/">Pandas: split one column into two or multiple columns  in Python</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>In today&#8217;s quick tutorial we&#8217;ll learn how to re-format your column contents so that you can split data located in a DataFrame column into one or more columns.</p>



<p>Most probably you&#8217;ll be acquiring your data from an API, database, text or comma separated value file. But in this example we&#8217;ll use a simple dataframe that we&#8217;ll define manually out of a dictionary.</p>



<pre class="wp-block-code"><code># Python3
import pandas as pd
targets = pd.DataFrame({ "manager": &#91;"Johns;Tim ", "Mcgregor; Dave", "DeRocca; Leo", "Haze; Jim"] ,
                     "target": &#91;42000, 85000, 45000, 33000]})</code></pre>



<p>Let&#8217;s look at the data:</p>



<pre class="wp-block-code"><code>targets.head()</code></pre>



<figure class="wp-block-table"><table><thead><tr><th></th><th>manager</th><th>target</th></tr></thead><tbody><tr><th>0</th><td>Johns;Tim</td><td>42000</td></tr><tr><th>1</th><td>Mcgregor; Dave</td><td>85000</td></tr><tr><th>2</th><td>DeRocca; Leo</td><td>45000</td></tr><tr><th>3</th><td>Haze; Jim</td><td>33000</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">Split columns in Pandas dataframes</h2>



<h3 class="wp-block-heading">Splitting into multiple columns with str.split()</h3>



<p>We would like to split the manager column into two. As you can see the name and family name are separated by a semi-colon.</p>



<pre class="wp-block-code"><code>manager = targets&#91;'manager']
targets&#91;&#91;'last_name','first_name']] =  manager.str.split(";", n=1, expand=True)
targets</code></pre>



<p>Explanation:</p>



<ol class="wp-block-list"><li>We user str.split() method to first convert the Series to a string.</li><li>The str.split() method receives a couple of parameters:<ul><li>First (&#8216;;&#8217;) is the split character, in this case a semi-colon.</li><li>Second (N=1) is the number of splits we would like to define for the string. If you would to divide the column text into more than two columns, <strong>set your N parameter accordingly</strong>.</li><li>Expand=True has to be specified, as otherwise the string will not be divided into different columns.</li></ul></li><li>Once split the strings are kept in two columns we&#8217;ll add to the dataframe: &#8216;last_name&#8217;,&#8217;first_name&#8217;</li></ol>



<p>Here&#8217;s our DataFrame:</p>



<figure class="wp-block-table"><table><thead><tr><th></th><th>manager</th><th>target</th><th>last_name</th><th>first_name</th></tr></thead><tbody><tr><th>0</th><td>Johns;Tim</td><td>42000</td><td>Johns</td><td>Tim</td></tr><tr><th>1</th><td>Mcgregor; Dave</td><td>85000</td><td>Mcgregor</td><td>Dave</td></tr><tr><th>2</th><td>DeRocca; Leo</td><td>45000</td><td>DeRocca</td><td>Leo</td></tr><tr><th>3</th><td>Haze; Jim</td><td>33000</td><td>Haze</td><td>Ji</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">Dividing a DataFrame column by comma</h3>



<p>If your separator is a comma, then we just need to adjust the separator parameter of the split method.</p>



<pre class="wp-block-code"><code>targets&#91;&#91;'last_name','first_name']] =  manager.str.split(<strong>","</strong>, n=1, expand=True)</code></pre>



<h3 class="wp-block-heading">Export DataFrame Column values to list</h3>



<p>Couple of readers asked me about this topic. We have a detailed post on this topic. <a href="https://www.dataforeverybody.com/how-to-export-a-python-dataframe-column-to-a-list/">Look here</a>.</p>



<h3 class="wp-block-heading">Drop a redundant Pandas column</h3>



<p> if you would like to keep only one of the new columns we just created, you can use the following code:</p>



<pre class="wp-block-code"><code>targets.drop('first_name', axis=1)</code></pre>



<p>More on removing <a href="https://www.dataforeverybody.com/pandas-drop-remove-column-index-value/">Pandas dataframe columns can be found here</a>.</p>


<h2>Common Questions</h2>
<h4>How do I split a column when values have inconsistent delimiters?</h4>
<p>Pass a regex pattern to <code>str.split()</code> to handle multiple delimiters simultaneously. For instance, <code>df['col'].str.split(r'[;,|]', expand=True)</code> splits on semicolons, commas, or pipes in a single operation. Set <code>expand=True</code> to place each piece into its own column automatically. When the split produces fewer pieces for some rows, Pandas fills the missing columns with <code>NaN</code>. You can then <a href="/empty-column-pandas-python-example/">insert an empty column into your dataframe</a> as a placeholder before merging the split results back in, giving you full control over column ordering.</p>
<h4>How do I keep the original column after splitting it into multiple new columns?</h4>
<p>The <code>str.split(expand=True)</code> method returns a new dataframe and does not modify the original column. Assign the result to new column names like <code>df[['first', 'last']] = df['name'].str.split(' ', n=1, expand=True)</code>, and the source column remains intact. If you need to build up columns from list-type data stored in cells, you can <a href="/append-python-list-columns-pandas/">append Python list values as new columns</a> using similar assignment patterns. Setting the <code>n</code> parameter limits the number of splits, which is useful when only the first delimiter matters.</p>
<h4>Why does str.split() return NaN for some rows?</h4>
<p>This happens when a cell contains a genuine <code>NaN</code> value rather than a string. The <code>str.split()</code> method skips non-string entries and returns <code>NaN</code> for those rows. Clean your data first with <code>df['col'].fillna('')</code> to convert missing values to empty strings before splitting. Alternatively, use <code>df.dropna(subset=['col'])</code> to remove rows with missing data entirely if they are not relevant to your analysis.</p><p>The post <a href="https://www.dataforeverybody.com/pandas-split-columns-dataframe/">Pandas: split one column into two or multiple columns  in Python</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.dataforeverybody.com/pandas-split-columns-dataframe/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Seaborn and Matplotlib axis limits with xlim and ylim</title>
		<link>https://www.dataforeverybody.com/seaborn-matplotlib-axis-limits/</link>
					<comments>https://www.dataforeverybody.com/seaborn-matplotlib-axis-limits/#comments</comments>
		
		<dc:creator><![CDATA[Gili]]></dc:creator>
		<pubDate>Sat, 28 Nov 2020 17:46:14 +0000</pubDate>
				<category><![CDATA[Data Visualization]]></category>
		<guid isPermaLink="false">https://www.dataforeverybody.com/?p=530</guid>

					<description><![CDATA[<p>In this quick recipe we&#8217;ll learn how to define axis limits for our Seaborn and Matplotlib plots in Python. For convenience, I&#8217;ll use Seaborn in ... </p>
<p class="read-more-container"><a title="Seaborn and Matplotlib axis limits with xlim and ylim" class="read-more button" href="https://www.dataforeverybody.com/seaborn-matplotlib-axis-limits/#more-530" aria-label="More on Seaborn and Matplotlib axis limits with xlim and ylim">Read more</a></p>
<p>The post <a href="https://www.dataforeverybody.com/seaborn-matplotlib-axis-limits/">Seaborn and Matplotlib axis limits with xlim and ylim</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>In this quick recipe we&#8217;ll learn how to define axis limits for our Seaborn and Matplotlib plots in Python.</p>
<p>For convenience, I&#8217;ll use Seaborn in this example, but the methods we&#8217;ll use in order to resize the axis limits are first and foremost part of Matplotlib and can be used on every pyplot chart.</p>
<h2 class="wp-block-heading">Use Seaborn xlim and set_ylim to set axis limits</h2>
<p>Consider the following code that will render the simple scatter plot we see below.</p>
<pre class="wp-block-code"><code>fig, scatter = plt.subplots(figsize = (10,6), dpi = 100)
scatter = sns.scatterplot(x = 'mass', y ='distance', data=data);</code></pre>
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="893" height="513" src="https://www.dataforeverybody.com/wp-content/uploads/2020/11/matplot_axes_limits.png" alt="img[matplot_axes_limits.png] alt='Matplotlib plot with custom axis limits example" class="wp-image-533" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/11/matplot_axes_limits.png 893w, https://www.dataforeverybody.com/wp-content/uploads/2020/11/matplot_axes_limits-300x172.png 300w, https://www.dataforeverybody.com/wp-content/uploads/2020/11/matplot_axes_limits-768x441.png 768w" sizes="auto, (max-width: 893px) 100vw, 893px" /></figure>
<p>Seems that except a few outliers, we can probably focus our data analysis on the bottom left part of our sns.scatter plot chart.</p>
<p>If that is the case, we can go ahead and redefine new limits for both axes in our plot using the following script.</p>
<pre class="wp-block-code"><code># with xlim/ylim
fig, scatter = plt.subplots(figsize = (10,6), dpi = 100)
scatter = sns.scatterplot(x = 'mass', y ='distance', data=data)
scatter.<strong>set_xlim</strong>(left=0, right=6)
scatter.<strong>set_ylim</strong>(bottom=0, top=200);</code></pre>
<p>Here&#8217;s our chart, note the new range limits for both the X and y axes. Note that for better clarity you might want to <a href="https://www.dataforeverybody.com/seaborn-plot-figure-size/">resize your Seaborn plot</a> as needed.</p>
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="879" height="518" src="https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_set_axis_limits.png" alt="img[seaborn_set_axis_limits.png] alt='Seaborn chart with xlim and ylim axis range set" class="wp-image-535" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_set_axis_limits.png 879w, https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_set_axis_limits-300x177.png 300w, https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_set_axis_limits-768x453.png 768w" sizes="auto, (max-width: 879px) 100vw, 879px" /></figure>
<p>Important Note: Although we used an sns.scatterplot as an example, all the above works on the other Seaborn charts: boxplot, barplot, catplot, lineplot etc&#8217;</p>
<p>Follow up questions? Happy to answer your comments.</p><p>The post <a href="https://www.dataforeverybody.com/seaborn-matplotlib-axis-limits/">Seaborn and Matplotlib axis limits with xlim and ylim</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.dataforeverybody.com/seaborn-matplotlib-axis-limits/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>How to export a Python Dataframe column to a list?</title>
		<link>https://www.dataforeverybody.com/how-to-export-a-python-dataframe-column-to-a-list/</link>
					<comments>https://www.dataforeverybody.com/how-to-export-a-python-dataframe-column-to-a-list/#respond</comments>
		
		<dc:creator><![CDATA[Gili]]></dc:creator>
		<pubDate>Sat, 28 Nov 2020 13:15:05 +0000</pubDate>
				<category><![CDATA[Data Wrangling]]></category>
		<guid isPermaLink="false">https://www.dataforeverybody.com/?p=543</guid>

					<description><![CDATA[<p>This quick tutorial will cover the topic of writing a DataFrame column to a Python list. Consider the following code snippet: Here&#8217;s our DataFrame header: ... </p>
<p class="read-more-container"><a title="How to export a Python Dataframe column to a list?" class="read-more button" href="https://www.dataforeverybody.com/how-to-export-a-python-dataframe-column-to-a-list/#more-543" aria-label="More on How to export a Python Dataframe column to a list?">Read more</a></p>
<p>The post <a href="https://www.dataforeverybody.com/how-to-export-a-python-dataframe-column-to-a-list/">How to export a Python Dataframe column to a list?</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>This quick tutorial will cover the topic of writing a DataFrame column to a Python list. </p>



<p>Consider the following code snippet:</p>



<pre class="wp-block-code"><code># import the pandas package
import pandas as pd

# initialize the DataFrame
data = pd.DataFrame({ "manager": ["Debbie", "Daisy", "Dorothy", "Tim"] * 2,
                     "target": [32000, 45000, 18000, 20000] * 2})
data.head()</code></pre>



<p>Here&#8217;s our DataFrame header:</p>



<figure class="wp-block-table"><table class=""><thead><tr><th></th><th>manager</th><th>target</th></tr></thead><tbody><tr><th>0</th><td>Debbie</td><td>32000</td></tr><tr><th>1</th><td>Daisy</td><td>45000</td></tr><tr><th>2</th><td>Dorothy</td><td>18000</td></tr><tr><th>3</th><td>Tim</td><td>20000</td></tr><tr><th>4</th><td>Debbie</td><td>32000</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">Using pd.DataFrame.tolist() to write column to a list</h2>



<p>Here&#8217;s the Python code you&#8217;ll need to export column values to a Python list:</p>



<pre class="wp-block-code"><code># Dataframe Column values to Python list
data['manager'].values.tolist()</code></pre>



<p>Here&#8217;s the list object we&#8217;ll get:</p>



<p>[&#8216;Debbie&#8217;, &#8216;Daisy&#8217;, &#8216;Dorothy&#8217;, &#8216;Tim&#8217;, &#8216;Debbie&#8217;, &#8216;Daisy&#8217;, &#8216;Dorothy&#8217;, &#8216;Tim&#8217;]</p>



<h3 class="wp-block-heading">Pandas column index to list</h3>



<pre class="wp-block-code"><code>data['manager'].index.tolist()</code></pre>



<p>[0, 1, 2, 3, 4, 5, 6, 7]</p>



<h3 class="wp-block-heading">Unique column values to list</h3>



<p>As shown above, we were able to export the values to a list. The list however had duplicated entries. Here&#8217;s how to ensure that the list has only unique values:</p>



<pre class="wp-block-code"><code># Export unique column values only
data['manager'].unique().tolist()</code></pre>



<p>[&#8216;Debbie&#8217;, &#8216;Daisy&#8217;, &#8216;Dorothy&#8217;, &#8216;Tim&#8217;]</p>



<h4 class="wp-block-heading">Unique list values using set</h4>



<p>Other way to ensure uniqueness in the list is to convert it to a set to get rid of duplicated values.</p>



<pre class="wp-block-code"><code>unique_managers = set(data['manager'].values.tolist())
list(unique_managers)
</code></pre>



<h3 class="wp-block-heading">tolist() with condition</h3>



<p>What if we just want to write values that satisfy a condition?</p>



<pre class="wp-block-code"><code># Condition: Only managers which name starts with the letter D
cond = data['manager'].str.startswith('D')
data[cond]['manager'].unique().tolist()</code></pre>



<p>Here&#8217;s the result:</p>



<p>[&#8216;Debbie&#8217;, &#8216;Daisy&#8217;, &#8216;Dorothy&#8217;]</p>



<h3 class="wp-block-heading">Series values to List</h3>



<p>For completeness, here&#8217;s a short snippet for writing Series values to a list (you can find a more <a href="https://www.dataforeverybody.com/convert-pandas-series-python-list/">complete example here</a>)</p>



<pre class="wp-block-code"><code>manager_series = pd.Series(["Debbie", "Kim", "Dorothy", "Tim"])
manager_series.to_list()</code></pre>


<h2>Going Further</h2>
<h4>Can I export a DataFrame index to a list the same way as a column?</h4>
<p>The approach is nearly identical. Use <code>df.index.tolist()</code> to convert the index into a standard Python list. This works for default integer indices, named indices, and DatetimeIndex objects alike. If your DataFrame uses a MultiIndex, <code>tolist()</code> returns a list of tuples representing each level combination. For a deeper walkthrough of index conversion scenarios, including exporting to NumPy arrays, see the guide on <a href="/convert-pandas-index-list-array/">converting a Pandas index to a list or array</a>. The method is consistent across Pandas versions and handles missing index values gracefully by preserving NaN entries in the output list.</p>
<h4>How do I export only unique column values or filter rows before converting?</h4>
<p>Chain <code>.unique().tolist()</code> on a column to extract deduplicated values — for example, <code>df['city'].unique().tolist()</code> returns distinct city names in order of first appearance. For conditional exports, apply a boolean mask first: <code>df[df['sales'] > 100]['product'].tolist()</code> gives you product names only where sales exceed 100. When filtering by specific row values across multiple conditions, techniques for <a href="/pandas-find-dataframe-row-value/">finding DataFrame rows by value</a> pair well with <code>tolist()</code> to build precise, filtered lists from your data.</p>
<h4>What is the performance difference between tolist() and list() on a DataFrame column?</h4>
<p>Both <code>df['col'].tolist()</code> and <code>list(df['col'])</code> produce identical output, but <code>tolist()</code> is faster for large DataFrames because it operates directly on the underlying NumPy array without Python-level iteration. On a column with one million rows, <code>tolist()</code> typically runs two to three times faster. The difference becomes negligible below ten thousand rows. Also note that <code>tolist()</code> converts NumPy data types to native Python types automatically, which matters when serializing to JSON or passing values to libraries that reject NumPy scalars.</p><p>The post <a href="https://www.dataforeverybody.com/how-to-export-a-python-dataframe-column-to-a-list/">How to export a Python Dataframe column to a list?</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.dataforeverybody.com/how-to-export-a-python-dataframe-column-to-a-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Pandas: how to plot timeseries based charts?</title>
		<link>https://www.dataforeverybody.com/pandas-plot-datetime-timeseries/</link>
					<comments>https://www.dataforeverybody.com/pandas-plot-datetime-timeseries/#respond</comments>
		
		<dc:creator><![CDATA[Gili]]></dc:creator>
		<pubDate>Sat, 28 Nov 2020 10:23:16 +0000</pubDate>
				<category><![CDATA[Data Visualization]]></category>
		<guid isPermaLink="false">https://www.dataforeverybody.com/?p=537</guid>

					<description><![CDATA[<p>In this quick recipe we&#8217;ll learn how to quickly create a chart that displays time series key performance indicator data. Let&#8217;s quickly start by creating ... </p>
<p class="read-more-container"><a title="Pandas: how to plot timeseries based charts?" class="read-more button" href="https://www.dataforeverybody.com/pandas-plot-datetime-timeseries/#more-537" aria-label="More on Pandas: how to plot timeseries based charts?">Read more</a></p>
<p>The post <a href="https://www.dataforeverybody.com/pandas-plot-datetime-timeseries/">Pandas: how to plot timeseries based charts?</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>In this quick recipe we&#8217;ll learn how to quickly create a chart that displays time series key performance indicator data.</p>
<p>Let&#8217;s quickly start by creating some random data for this exercise:</p>
<pre class="wp-block-code"><code>import pandas as pd
import numpy as np # used for generating random data
np.random.seed(10)

date_range =  pd.date_range('2020-01-01','2020-11-01', freq ='MS')

raw_data = {'time': date_range, \
                     'sales': &#91;x for x in np.random.randint(30, 100, \
                               len(date_range) )]}

# create dataframe from dictionary

sales = pd.DataFrame.from_dict(raw_data)

sales.head()</code></pre>
<p>Here&#8217;s our dataframe header:</p>
<figure class="wp-block-table">
<table class="">
<thead>
<tr>
<th></th>
<th>time</th>
<th>sales</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>2020-01-01</td>
<td>79</td>
</tr>
<tr>
<th>1</th>
<td>2020-02-01</td>
<td>91</td>
</tr>
<tr>
<th>2</th>
<td>2020-03-01</td>
<td>87</td>
</tr>
<tr>
<th>3</th>
<td>2020-04-01</td>
<td>74</td>
</tr>
<tr>
<th>4</th>
<td>2020-05-01</td>
<td>66</td>
</tr>
</tbody>
</table>
</figure>
<h3 class="wp-block-heading">Using DataFrame.plot() to draw datetime charts in Pandas</h3>
<p>Now that we have some data available, let&#8217;s take a look at how to quickly draw our plot using the DataFrame.plot() method that is readily made available in Pandas.</p>
<pre class="wp-block-code"><code>sales.plot(x= 'time', y= 'sales', kind='line');</code></pre>
<p>This will render a simple line plot. Pandas took care of converting the datetime values of the &#8216;time&#8217; column to months automatically.</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="399" height="278" src="https://www.dataforeverybody.com/wp-content/uploads/2020/11/pands_datetime_plot.png" alt="img[pands_datetime_plot.png] alt='Pandas datetime plot showing timeseries data visualization" class="wp-image-539" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/11/pands_datetime_plot.png 399w, https://www.dataforeverybody.com/wp-content/uploads/2020/11/pands_datetime_plot-300x209.png 300w" sizes="auto, (max-width: 399px) 100vw, 399px" /></figure>
</div>
<p>And a  bit more elaborated version:</p>
<pre class="wp-block-code"><code>sales.plot(x= 'time', 
           y= 'sales', 
           kind='line',
           figsize = (10,6), 
           title="Sales Over Time",
           grid=True , 
           style = 'r');</code></pre>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="652" height="398" src="https://www.dataforeverybody.com/wp-content/uploads/2020/11/pands_timeseries_plot.png" alt="img[pands_timeseries_plot.png] alt='Pandas timeseries line chart with date axis example" class="wp-image-540" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/11/pands_timeseries_plot.png 652w, https://www.dataforeverybody.com/wp-content/uploads/2020/11/pands_timeseries_plot-300x183.png 300w" sizes="auto, (max-width: 652px) 100vw, 652px" /></figure>
</div><p>The post <a href="https://www.dataforeverybody.com/pandas-plot-datetime-timeseries/">Pandas: how to plot timeseries based charts?</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.dataforeverybody.com/pandas-plot-datetime-timeseries/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Fix Seaborn and Matplotlib plots not showing in Jupyter</title>
		<link>https://www.dataforeverybody.com/show-seaborn-matplotlib-plot/</link>
					<comments>https://www.dataforeverybody.com/show-seaborn-matplotlib-plot/#respond</comments>
		
		<dc:creator><![CDATA[Gili]]></dc:creator>
		<pubDate>Fri, 27 Nov 2020 09:29:40 +0000</pubDate>
				<category><![CDATA[Data Visualization]]></category>
		<guid isPermaLink="false">https://www.dataforeverybody.com/?p=521</guid>

					<description><![CDATA[<p>The other day, a reader asked me the following question: I am getting started with using Matplotlib and Seaborn on a Jupyter notebook. Trying to ... </p>
<p class="read-more-container"><a title="Fix Seaborn and Matplotlib plots not showing in Jupyter" class="read-more button" href="https://www.dataforeverybody.com/show-seaborn-matplotlib-plot/#more-521" aria-label="More on Fix Seaborn and Matplotlib plots not showing in Jupyter">Read more</a></p>
<p>The post <a href="https://www.dataforeverybody.com/show-seaborn-matplotlib-plot/">Fix Seaborn and Matplotlib plots not showing in Jupyter</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>The other day, a reader asked me the following question:</p>
<p><em>I am getting started with using Matplotlib and Seaborn on a Jupyter notebook. Trying to show a simple scatter plot but nothing shows up. What do you think i am doing wrong so no chart is displayed ?</em></p>
<p>It&#8217;s a bit hard to help without the code sample, but here are a couple of pointers as well as a simple example that you can use to troubleshoot this.</p>
<h3 class="wp-block-heading">Set %matplotlib inline</h3>
<p>In order to plot directly into your Jupyter notebook, you need to set the following swtich a=fter importing Matplotlib.</p>
<pre class="wp-block-code"><code>import matplotlib.pyplot as plt
%matplotlib inline</code></pre>
<h3 class="wp-block-heading">Use pyplot.show() to display your charts</h3>
<p>Here&#8217;s a code snippet that uses the built-in Seaborn planets dataset for simplicity.</p>
<pre class="wp-block-code"><code>import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

my_data = sns.load_dataset ('planets')

sns.scatterplot(x = 'mass', y ='distance', data=my_data)
plt.show()</code></pre>
<p>The plt.show method will display your scatter chart:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="411" height="268" src="https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_show_plot.png" alt="Seaborn plot displayed in Jupyter notebook using plt.show()" class="wp-image-523" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_show_plot.png 411w, https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_show_plot-300x196.png 300w" sizes="auto, (max-width: 411px) 100vw, 411px" /></figure>
</div>
<p>From here you can go ahead and customize your chart such as adding <a href="https://www.dataforeverybody.com/change-seaborn-title-chart-axes-font/">titles</a> and l<a href="https://www.dataforeverybody.com/seaborn-legend-change-location-size/">egends</a>. </p>
<p>Hopefully this helped answer your question. Feel free to leave us additional questions in the comments below.</p><p>The post <a href="https://www.dataforeverybody.com/show-seaborn-matplotlib-plot/">Fix Seaborn and Matplotlib plots not showing in Jupyter</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.dataforeverybody.com/show-seaborn-matplotlib-plot/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Rotate axis tick labels in Seaborn and Matplotlib</title>
		<link>https://www.dataforeverybody.com/matplotlib-seaborn-rotate-xticklabels/</link>
					<comments>https://www.dataforeverybody.com/matplotlib-seaborn-rotate-xticklabels/#respond</comments>
		
		<dc:creator><![CDATA[Gili]]></dc:creator>
		<pubDate>Fri, 27 Nov 2020 08:57:33 +0000</pubDate>
				<category><![CDATA[Data Visualization]]></category>
		<guid isPermaLink="false">https://www.dataforeverybody.com/?p=513</guid>

					<description><![CDATA[<p>In today&#8217;s quick tutorial we&#8217;ll cover the basics of labels rotation in Seaborn and Matplotlib. For convenience examples will be based on Seaborn charts, but ... </p>
<p class="read-more-container"><a title="Rotate axis tick labels in Seaborn and Matplotlib" class="read-more button" href="https://www.dataforeverybody.com/matplotlib-seaborn-rotate-xticklabels/#more-513" aria-label="More on Rotate axis tick labels in Seaborn and Matplotlib">Read more</a></p>
<p>The post <a href="https://www.dataforeverybody.com/matplotlib-seaborn-rotate-xticklabels/">Rotate axis tick labels in Seaborn and Matplotlib</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>In today&#8217;s quick tutorial we&#8217;ll cover the basics of labels rotation in Seaborn and Matplotlib. For convenience examples will be based on Seaborn charts, but they are fully relevant to Matplotlib.</p>
<p>Here&#8217;s a Python snippet that builds a simple Seaborn barplot (sns.barplot). I assume that you have already imported Matplotlib and / or Seaborn to your Jupyter notebook beforehand.</p>
<pre class="wp-block-code"><code>

labels = &#91;'Grocery deliveries', 'Gear deliveries', 'Food deliveries', 'Medicines deliveries']

fig, bar = plt.subplots()
bar = sns.barplot(data = deliveries, x = x, y = y, estimator = sum)
bar.set_ylabel('Tip', fontsize=14)
bar.set_xlabel('Type', fontsize=14)
bar.set_xticklabels(labels, fontsize=14);</code></pre>
<p>Here&#8217;s the result:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="446" height="283" src="https://www.dataforeverybody.com/wp-content/uploads/2020/11/rotate_seaborn_boxplot.png" alt="Seaborn plot with rotated x-axis tick labels" class="wp-image-516" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/11/rotate_seaborn_boxplot.png 446w, https://www.dataforeverybody.com/wp-content/uploads/2020/11/rotate_seaborn_boxplot-300x190.png 300w" sizes="auto, (max-width: 446px) 100vw, 446px" /></figure>
</div>
<p>The axes ticks xticklabels are overlapping and not readable.</p>
<h2 class="wp-block-heading">Rotate Matplotlib and Seaborn tick labels</h2>
<p>The solution is relatively simple. We need to use the rotation parameter that is available for the <strong><em>pyplot.xticklabels</em></strong> method. For eachset of tick labels, you&#8217;ll need to specify the required rotation angle in degrees.</p>
<pre class="wp-block-code"><code>bar.set_xticklabels(labels, fontsize=14, rotation=30);</code></pre>
<p>You might want to use the ha parameter to horizontally align the label.</p>
<pre class="wp-block-code"><code>bar.set_xticklabels(labels, fontsize=14, rotation=30, ha= 'right');</code></pre>
<p>Here&#8217;s the updated chart:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="501" height="337" src="https://www.dataforeverybody.com/wp-content/uploads/2020/11/matplotlib_rotate_labels.png" alt="Matplotlib chart showing rotated axis labels at 45 degrees" class="wp-image-517" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/11/matplotlib_rotate_labels.png 501w, https://www.dataforeverybody.com/wp-content/uploads/2020/11/matplotlib_rotate_labels-300x202.png 300w" sizes="auto, (max-width: 501px) 100vw, 501px" /></figure>
</div>
<p>The procedure to rotate and align the y axes is similar &#8211; just use the pyplot.set_yticklabels() method instead.</p>
<p>Tip: as you see, the chart is still somewhat small and not readable enough. If that&#8217;s the case, you can <a href="https://www.dataforeverybody.com/seaborn-plot-figure-size/">increase your plot size</a> with ease.</p>
<p>This procedure is identical for lineplots, boxplots, catplots and heatmaps. </p><p>The post <a href="https://www.dataforeverybody.com/matplotlib-seaborn-rotate-xticklabels/">Rotate axis tick labels in Seaborn and Matplotlib</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.dataforeverybody.com/matplotlib-seaborn-rotate-xticklabels/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to change Seaborn legends font size, location and color?</title>
		<link>https://www.dataforeverybody.com/seaborn-legend-change-location-size/</link>
					<comments>https://www.dataforeverybody.com/seaborn-legend-change-location-size/#respond</comments>
		
		<dc:creator><![CDATA[Gili]]></dc:creator>
		<pubDate>Fri, 27 Nov 2020 08:18:30 +0000</pubDate>
				<category><![CDATA[Data Visualization]]></category>
		<guid isPermaLink="false">https://www.dataforeverybody.com/?p=505</guid>

					<description><![CDATA[<p>In this tutorial we&#8217;ll learn about how to set and change legends in Python Seaborn charts. We&#8217;ll go through an end to end example which ... </p>
<p class="read-more-container"><a title="How to change Seaborn legends font size, location and color?" class="read-more button" href="https://www.dataforeverybody.com/seaborn-legend-change-location-size/#more-505" aria-label="More on How to change Seaborn legends font size, location and color?">Read more</a></p>
<p>The post <a href="https://www.dataforeverybody.com/seaborn-legend-change-location-size/">How to change Seaborn legends font size, location and color?</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>In this tutorial we&#8217;ll learn about how to set and change legends in Python Seaborn charts. We&#8217;ll go through an end to end example which you might want to follow.</p>
<p>We&#8217;ll be using our deliveries dataset, which you can <a href="https://www.dataforeverybody.com/wp-content/uploads/2020/11/deliveries.csv" target="_blank" rel="noreferrer noopener">download from here</a>. </p>
<h2 class="wp-block-heading">Seaborn legends example</h2>
<p>Consider the following script that renders a scatter plot from a Python DataFrame data.</p>
<pre class="wp-block-code"><code># import required libraries
import seaborn as sns
import pandas as pd

sns.set_style('whitegrid')

# acquire the test data
deliveries = pd.read_csv('../data/del_tips.csv')

# define the axes content
x = deliveries&#91;'time_to_deliver']
y = deliveries&#91;'del_tip_amount']

#sns.scatterplot from DataFrame
fig, scatter = plt.subplots(figsize = (10,6), dpi = 100)
scatter = sns.scatterplot(x = x, y =y, data=deliveries, hue='type' )
scatter.set_title('Tip vs Delivery Speed', fontsize = 23, y =1.05);</code></pre>
<p>We&#8217;ll get the following chart:</p>
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="878" height="587" src="https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_legend_location.png" alt="Seaborn scatterplot with customized legend position" class="wp-image-508" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_legend_location.png 878w, https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_legend_location-300x201.png 300w, https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_legend_location-768x513.png 768w" sizes="auto, (max-width: 878px) 100vw, 878px" /></figure>
<h3 class="wp-block-heading">Hide the Seaborn legend</h3>
<p>If you might want to remove your legend altogether, you need to use the <strong><em>legend=False</em></strong> switch.</p>
<pre class="wp-block-code"><code>scatter = sns.scatterplot(x = x, y =y, data=deliveries, hue='type', legend= False)</code></pre>
<p>Seaborn will display the following warning: <em>No handles with labels found to put in legend</em>.</p>
<h3 class="wp-block-heading">Change Seaborn legend location</h3>
<p>Probably the most visible issue we have with our chart is the location of the legend. What if we want to display it outside the figure, so it doesn&#8217;t overlap with any observation we plot? </p>
<p>The bbox_to_anchor parameter allows us to pass an (x,y) tuple with the required offset. You might want to tweak it as per your needs.</p>
<pre class="wp-block-code"><code>scatter.legend(bbox_to_anchor= (1.03, 1) );</code></pre>
<p>Note: We can also use the loc parameter to specify a location for the legend size on the the chart. I typically use <em>loc= &#8216;upper right&#8217;</em>.</p>
<h3 class="wp-block-heading">Set legend font size</h3>
<p>The second issue we recognize in our chart is that by default our legend fonts are too small for the figsize we set. We can fix it with ease:</p>
<pre class="wp-block-code"><code>#tweak the font heights in px
scatter.legend(fontsize = 15) </code></pre>
<h3 class="wp-block-heading">Modify the Seaborn legend title</h3>
<p>Next we would like to provide a meaningful title to our legend.</p>
<pre class="wp-block-code"><code>scatter.legend(title="Delivery Type");</code></pre>
<h3 class="wp-block-heading">Set the Legend background color</h3>
<p>We can also modify the background color of our legend, as shown below:</p>
<pre class="wp-block-code"><code>scatter.legend(shadow = True, facecolor = 'grey')</code></pre>
<h3 class="wp-block-heading">Bringing all of it together</h3>
<pre class="wp-block-code"><code>scatter.legend(fontsize = 15, \
               bbox_to_anchor= (1.03, 1), \
               title="Delivery Type", \
               title_fontsize = 18, \
               shadow = True, \
               facecolor = 'white');</code></pre>
<figure class="wp-block-gallery columns-1 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex">
<ul class="blocks-gallery-grid">
<li class="blocks-gallery-item">
<figure><img loading="lazy" decoding="async" width="1024" height="547" src="https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_legend_size_font-1024x547.png" alt="Seaborn plot legend with modified font size and background color" data-id="510" data-full-url="https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_legend_size_font.png" data-link="https://www.dataforeverybody.com/?attachment_id=510#main" class="wp-image-510" srcset="https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_legend_size_font-1024x547.png 1024w, https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_legend_size_font-300x160.png 300w, https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_legend_size_font-768x411.png 768w, https://www.dataforeverybody.com/wp-content/uploads/2020/11/seaborn_legend_size_font.png 1070w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>
</li>
</ul>
</figure>
<p>Notes: </p>
<ul>
<li>There is more to customize for Seaborn legends (handlers, labels etc&#8217;). We&#8217;ll cover that in subsequent tutorials.</li>
<li>Although written for the scatterplot, this tutorial is applicable for the different Seaborn charts for raw data, bi-variate / categorical analysis: catplot, plotbox, barplot, paiplots etc&#8217;. </li>
</ul><p>The post <a href="https://www.dataforeverybody.com/seaborn-legend-change-location-size/">How to change Seaborn legends font size, location and color?</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.dataforeverybody.com/seaborn-legend-change-location-size/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 

Served from: www.dataforeverybody.com @ 2026-06-15 18:05:43 by W3 Total Cache
-->