avatarBetter Everything

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

8267

Abstract

start at the origin (0,0)</h2><p id="43b9">When you plot lines or points on a graph, the ranges don’t start at 0 by default. Even when your data includes a point at (0,0).</p><figure id="6591"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*uXrM3PnLmT_StUYA7c-iDg.png"><figcaption></figcaption></figure><figure id="108d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*nfbYRwd8HJg2h5TyQa-NKQ.png"><figcaption>2 lines plotted in matplotlib charts that don’t start at the origin (0,0). Source: own images.</figcaption></figure><p id="4147">But this can be achieved with the xlim and ylim functions.</p><p id="ea41">To start the y range at 0 you use <code>plt.ylim(bottom=0)</code> and to start the x range at 0 you use <code>plt.xlim(left=0)</code>. These have to be used after plotting the data for it to work.</p><figure id="1273"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*-wQXP5JI4D0WdAm6DU4Zog.png"><figcaption></figcaption></figure><figure id="d474"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Kw__VAxbIVp0f02X4fos_w.png"><figcaption>2 lines plotted in matplotlib charts that start at the origin (0,0) because the xlim and ylim functions have been used. Source: own images.</figcaption></figure><p id="3bf6">If you use <code>fig, ax = plt.subplots</code>, you would use the set_xlim and set_ylim methods like this:</p><div id="5eed"><pre>ax.set_xlim(left=<span class="hljs-number">0</span>) ax.set_ylim(bottom=<span class="hljs-number">0</span>)</pre></div><h2 id="2103">4 Set the outer limits of the x and y ranges</h2><p id="f532">With the same xlim and ylim functions you can also define the outer limit of an axis’ range.</p><p id="47b3">To set the outer limit of the x range to 150 you can use <code>xlim(right=150)</code>. And to set the outer limit of the y range to 150 you can use <code>ylim(top=150)</code>.</p><p id="a9d9">And of course you can start a range at other values than 0 as well:</p><div id="d4b3"><pre>plt.title(<span class="hljs-string">'Team 1'</span>) plt.xlabel(<span class="hljs-string">'Height'</span>) plt.ylabel(<span class="hljs-string">'Weight'</span>)

plt.scatter([<span class="hljs-number">190</span>,<span class="hljs-number">185</span>,<span class="hljs-number">185</span>,<span class="hljs-number">175</span>,<span class="hljs-number">187</span>,<span class="hljs-number">179</span>],[<span class="hljs-number">80</span>,<span class="hljs-number">85</span>,<span class="hljs-number">67</span>,<span class="hljs-number">75</span>,<span class="hljs-number">83</span>,<span class="hljs-number">85</span>])

plt.xlim(left=<span class="hljs-number">150</span>, right=<span class="hljs-number">200</span>) plt.ylim(bottom=<span class="hljs-number">0</span>, top=<span class="hljs-number">100</span>)

plt.show()</pre></div><figure id="5343"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*rq0kqosX7-XPqGwm9IjcnA.png"><figcaption></figcaption></figure><figure id="5584"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*_7-lBDGDcko3G37TvKLrdQ.png"><figcaption>2 Scatterplots with matplotlib that show the same data, but one has custom limits set with xlim and ylim. Source: own images.</figcaption></figure><h2 id="1114">5 Add horizontal or vertical lines to charts</h2><p id="779f">Sometimes a horizontal or vertical line can help make a clear distinction.</p><p id="823c">Think about when you have a bar chart with both negative and positive values. By adding a horizontal line at y=0 you get a clear distinction between the negative and positive values and it looks better in my opinion.</p><p id="a225">A horizontal line can be made with the axhline function by passing it a y value, you can also pick a color: <code>plt.axhline(y=0, color=’black’)</code></p><p id="72e5">Vertical lines can be made with axvline in a similar way: <code>plt.axvline(x=2019, color=’black’)</code></p><figure id="51c1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Up8Q0GnYQKVFRgmsANUs7g.png"><figcaption></figcaption></figure><figure id="45a1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*S9o1wex5JLXHZ98-dlgntQ.png"><figcaption>2 matplotlib bar charts with the same data, but one has a horizontal line at 0. Source: own images.</figcaption></figure><p id="7ecc">If you use <code>fig, ax = plt.subplots</code>, you would use the axhline and axvline methods like this:</p><div id="5737"><pre>ax.axhline(y=<span class="hljs-number">0</span>) ax.axvline(x=<span class="hljs-number">0</span>)</pre></div><h2 id="d262">6 Customize the values along the X or Y axis</h2><p id="bdd8">In matplotlib charts, ticks are the labels that specify values at the X or Y axis. You can completely customize the ticks that you want to show along an axis.</p><p id="a034">Let’s look at some example code to make a bar chart that shows yearly revenue data:</p><div id="9311"><pre><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt

years = <span class="hljs-built_in">range</span>(<span class="hljs-number">2013</span>,<span class="hljs-number">2023</span>) revenues = [<span class="hljs-number">55000</span>, <span class="hljs-number">105000</span>, <span class="hljs-number">300000</span>, <span class="hljs-number">927000</span>, <span class="hljs-number">1405000</span>, <span class="hljs-number">4905000</span>, <span class="hljs-number">4723000</span>, <span class="hljs-number">7000000</span>, <span class="hljs-number">12000000</span>, <span class="hljs-number">15000000</span>] plt.bar(years,revenues,color=<span class="hljs-string">'green'</span>)

plt.title(<span class="hljs-string">'Yearly Revenue 2013-2022'</span>) plt.ylabel(<span class="hljs-string">'Revenue $'</span>) plt.xlabel(<span class="hljs-string">'Year'</span>)

plt.tight_layout() plt.show()</pre></div><p id="ce36">The code produces this bar chart:</p><figure id="66c7"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*rWWPx6yseTDCycD-ANyB2A.png"><figcaption>A matplotlib bar chart with default ticks. Source: own image.</figcaption></figure><p id="a6bf">As you can see not every year has gotten a tick on the x axis. And on the y axis we can see scientific notation numbers at intervals of 2 million dollars.</p><p id="1af3">To specify at which values you want a tick to appear you can use the xticks and yticks functions to which you pass a collection of values like a list or range.</p><p id="7c8e">To show each year we use the range <code>years</code>, by default ranges have a stepsize of 1. This means that every year will get a tick on the x-axis.</p><p id="d578">And to show a y-axis tick at each interval of $1 million, we make a new range <code>y_tick_values</code>. The range goes from 0 to 16 million with a stepsize of 1 million: <code>y_tick_values = range(0,16000000,1000000)</code></p><p id="38e7">Here is the same bar chart making code after the xticks and yticks update:</p><div id="2293"><pre><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt

years = <span class="hljs-built_in">range</span>(<span class="hljs-number">2013</span>,<span class="hljs-number">2023</span>) revenues = [<span class="hljs-number">55000</span>, <span class="hljs-number">105000</span>, <span class="hljs-number">300000</span>, <span class="hljs-number">927000</span>, <span class="hljs-number">1405000</span>, <span class="hljs-number">4905000</span>, <span class="hljs-number">4723000</span>, <span class="hljs-number">7000000</span>, <span class="hljs-number">12000000</span>, <span class="hljs-number">15000000</span>] plt.bar(years,revenues,color=<span class="hljs-string">'green'</span>)

plt.xticks(years)

y_tick_values = <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>,<span class="hljs-number">16000000</span>,<span class="hljs-number">1000000</span>) plt.yticks(y_tick_values)

plt.title(<span class="hljs-string">'Yearly Revenue 2013-2022'</span>) plt.ylabel(<span class="hljs-string">'Revenue $'</span>) plt.xlabel(<span class="hljs-string">'Year'</span>)

plt.tight_layout() plt.show()</pre></div><p id="0b02">And here is the new bar chart:</p><figure id="

Options

2067"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*O7kwoEV0GWQm-788xGP1PQ.png"><figcaption>A matplotlib bar chart with customized ticks. Source: own image.</figcaption></figure><p id="1b21">Okay, so now we have ticks at the desired places, but how can we change what is displayed at these ticks? I don’t like the scientific notations along the y axis.</p><p id="3805">Changing the displayed labels at ticks can easily be done by specifying the desired labels as a second argument in the xticks or yticks function call.</p><p id="13ed">Here is an example:</p><div id="4384"><pre>y_tick_values = <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>,<span class="hljs-number">16000000</span>,<span class="hljs-number">1000000</span>) y_tick_labels = [<span class="hljs-string">'{:,}'</span>.<span class="hljs-built_in">format</span>(value) <span class="hljs-keyword">for</span> value <span class="hljs-keyword">in</span> y_tick_values] plt.yticks(y_tick_values,y_tick_labels)</pre></div><p id="2142">In the code above we make a list <code>y_tick_labels</code> that takes the elements from the <code>y_tick_values</code> range and transforms them.</p><p id="6ba5">With <code>'{:,}'.format(value)</code> a value gets transformed into a string that starts with and <code>:,</code> is to place commas as thousand separators. So that, for example, 12000000 will be12,000,000.</p><p id="74d9">Here is the new bar chart code:</p><div id="ac42"><pre><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt

years = <span class="hljs-built_in">range</span>(<span class="hljs-number">2013</span>,<span class="hljs-number">2023</span>) revenues = [<span class="hljs-number">55000</span>, <span class="hljs-number">105000</span>, <span class="hljs-number">300000</span>, <span class="hljs-number">927000</span>, <span class="hljs-number">1405000</span>, <span class="hljs-number">4905000</span>, <span class="hljs-number">4723000</span>, <span class="hljs-number">7000000</span>, <span class="hljs-number">12000000</span>, <span class="hljs-number">15000000</span>] plt.bar(years,revenues,color=<span class="hljs-string">'green'</span>)

plt.xticks(years)

y_tick_values = <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>,<span class="hljs-number">16000000</span>,<span class="hljs-number">1000000</span>) y_tick_labels = [<span class="hljs-string">'${:,}'</span>.<span class="hljs-built_in">format</span>(value) <span class="hljs-keyword">for</span> value <span class="hljs-keyword">in</span> y_tick_values] plt.yticks(y_tick_values,y_tick_labels)

plt.title(<span class="hljs-string">'Yearly Revenue 2013-2022'</span>) plt.ylabel(<span class="hljs-string">'Revenue $'</span>) plt.xlabel(<span class="hljs-string">'Year'</span>)

plt.tight_layout() plt.show()</pre></div><p id="d4cc">Now our bar chart looks like this:</p><figure id="64f5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*HeD9zQ5GYnryMzMr3dsT-g.png"><figcaption>A matplotlib bar chart with default tick values and tick labels. Source: own image.</figcaption></figure><p id="ba51">If you use <code>fig, ax = plt.subplots()</code> than you can use the set_xticks and set_yticks methods like this:</p><div id="5389"><pre>ax.set_xticks(years) ax.set_yticks(y_tick_values,y_tick_labels)</pre></div><h2 id="8716">7 Add a legend to your chart</h2><p id="2c7c">When you plot multiple series in a chart, adding a legend can help clarify what points belong to what entity.</p><p id="20d1">A legend can be made with the legend function.</p><p id="6d99">When you add the color and label argument to your series, for instance in a scatter plot, the legend function will map the serie’s colors to the labels automatically.</p><p id="00f6">Here is an example:</p><div id="c527"><pre>plt.title(<span class="hljs-string">'Team 1 vs Team 2'</span>) plt.xlabel(<span class="hljs-string">'Height'</span>) plt.ylabel(<span class="hljs-string">'Weight'</span>)

plt.scatter([<span class="hljs-number">190</span>,<span class="hljs-number">185</span>,<span class="hljs-number">185</span>,<span class="hljs-number">175</span>,<span class="hljs-number">187</span>,<span class="hljs-number">179</span>],[<span class="hljs-number">80</span>,<span class="hljs-number">85</span>,<span class="hljs-number">67</span>,<span class="hljs-number">75</span>,<span class="hljs-number">83</span>,<span class="hljs-number">85</span>], color=<span class="hljs-string">'red'</span>, label=<span class="hljs-string">'Team 1'</span>) plt.scatter([<span class="hljs-number">172</span>,<span class="hljs-number">185</span>,<span class="hljs-number">180</span>,<span class="hljs-number">175</span>,<span class="hljs-number">185</span>,<span class="hljs-number">179</span>],[<span class="hljs-number">70</span>,<span class="hljs-number">87</span>,<span class="hljs-number">84</span>,<span class="hljs-number">79</span>,<span class="hljs-number">86</span>,<span class="hljs-number">84</span>], color=<span class="hljs-string">'blue'</span>, label=<span class="hljs-string">'Team 2'</span>)

plt.legend(loc=<span class="hljs-string">'upper left'</span>) plt.<span class="hljs-keyword">show</span>()</pre></div><p id="db4f">And here is the chart:</p><figure id="3cae"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*6jXJxvJC1WUAU3c9sesW5Q.png"><figcaption>A matplotlib scatterplot with 2 series and a legend. Source: own image.</figcaption></figure><p id="6e65">If you use <code>fig, ax = plt.subplots()</code> than you can use the legend method like this:</p><div id="3099"><pre>ax.legend(loc=<span class="hljs-string">'upper left'</span>)</pre></div><h2 id="4273">8 Make charts in dark mode</h2><p id="6b5a">An easy way to change the background of your chart to black is by using the dark backhtound style. This way the things that were black like texts and lines automatically become white so that they stay visible.</p><p id="080c">This is how you can select the dark background style:</p><div id="10e6"><pre>plt.style.use(<span class="hljs-string">'dark_background'</span>)</pre></div><figure id="f25d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*KdgYLgjQXW1eIegSogLSGw.png"><figcaption></figcaption></figure><figure id="5425"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*DNudDAF6El1lUon2XoELuA.png"><figcaption>2 matplotlib bar charts that are the same but one has a black background and white text and lines because the dark background style was used. Source: own images.</figcaption></figure><h1 id="1d67">Thank you for reading!</h1><p id="8ee0">You can <b>get full access to all my posts by joining Medium</b>. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:</p><div id="80fc" class="link-block"> <a href="https://medium.com/@BetterEverything/membership"> <div> <div> <h2>Join Medium with my referral link — Better Everything</h2> <div><h3>Read every story from Better Everything (and thousands of other writers on Medium). Your membership fee directly…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*aa4Y_6MHVoY6Wl-9)"></div> </div> </div> </a> </div><p id="dc0a">More charts and graphs in Python:</p><div id="82de" class="link-block"> <a href="https://readmedium.com/beautiful-pie-charts-with-python-5f73cd79eccd"> <div> <div> <h2>Beautiful Pie Charts with Python</h2> <div><h3>Making pie charts in Python is fairly simple as you will soon see. It can be done with the matplotlib package which is…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*g6m82yCKemf8CaxajHP8-A.png)"></div> </div> </div> </a> </div></article></body>

8 Matplotlib Tips for Clear & Pretty Charts

Matplotlib is a popular package to make charts and visualize data. If you have ever tried matplotlib you might know that making charts with it is quite easy. But the default settings don’t always look that good.

In this article we will look at 8 tips for making clear & better looking charts.

1 Add a title and labels to your chart

Adding a title above your chart and a label to each axis is an easy way to make the chart more understandable. Descriptive titles and labels make it clear what data is visualized.

To add a title and axis labels you can use the title function. And to add a label to an axis you can use the xlabel or ylabel function:

import matplotlib.pyplot as plt

seasons = range(2000,2023)
wins = [10,9,15,8,15,1,9,9,8,1,5,1,3,2,0,3,0,5,6,3,0,0,4]
plt.bar(seasons,wins,color='red')

plt.title('Ferrari F1 Wins 2000-2022')
plt.ylabel('Number of Wins')
plt.xlabel('Seasons')

plt.show()
2 matplotlib bar charts that show the same data but one has a title and axis labels. Source: own images.

And if you use fig, ax = plt.subplots, you would use the set_title, set_xlabel and set_ylabel methods like this:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

seasons = range(2000,2023)
wins = [10,9,15,8,15,1,9,9,8,1,5,1,3,2,0,3,0,5,6,3,0,0,4]
ax.bar(seasons,wins,color='red')

ax.set_title('Ferrari F1 Wins 2000-2022')
ax.set_ylabel('Number of Wins')
ax.set_xlabel('Seasons')

plt.show()

You can also make style changes to the title and axis labels, like setting the fontsize with the fontsize argument:

plt.title('Ferrari F1 Wins 2000-2022',fontsize=24)
plt.ylabel('Number of Wins',fontsize=16)
plt.xlabel('Seasons',fontsize=16)

2 Prevent parts of your chart from being cut off

Sometimes the labels of your chart or other parts get cut off from the image. To prevent that, you can call the tight_layout function. I believe you have to call it just before using the show or save function to make sure it works.

net_income = {2018: [9000, 9000, 12000, 15000],
              2019: [-20000, 20000, 15000, 25000],
              2020: [7000, -3000, -9000, -10000],
              2021: [7000, 15000, 15000, 30000],
              2022: [15000, 15000, 25000, 40000]}

for year, data in net_income.items():
    x_coordinates = [year + 0.2 * q for q in [1,2,3,4]]
    plt.bar(x_coordinates,data,width=0.2,edgecolor='black')

years = net_income.keys()
plt.xticks([year+0.5 for year in years], years)

plt.title('Net Income per Quarter')
plt.ylabel('Net Income')
plt.xlabel('Years')

plt.axhline(y=0, color='black', linewidth=0.5)

plt.tight_layout()

plt.show()
2 matplotlib bar charts that are the same but one of them has its ylabel cut off. Source: own images.

The plt.tight_layout() function call also works if you use fig, ax = plt.subplots().

Learn to graph quarterly data like in the bar chart above with this article of mine:

3 Let the x and y ranges start at the origin (0,0)

When you plot lines or points on a graph, the ranges don’t start at 0 by default. Even when your data includes a point at (0,0).

2 lines plotted in matplotlib charts that don’t start at the origin (0,0). Source: own images.

But this can be achieved with the xlim and ylim functions.

To start the y range at 0 you use plt.ylim(bottom=0) and to start the x range at 0 you use plt.xlim(left=0). These have to be used after plotting the data for it to work.

2 lines plotted in matplotlib charts that start at the origin (0,0) because the xlim and ylim functions have been used. Source: own images.

If you use fig, ax = plt.subplots, you would use the set_xlim and set_ylim methods like this:

ax.set_xlim(left=0)
ax.set_ylim(bottom=0)

4 Set the outer limits of the x and y ranges

With the same xlim and ylim functions you can also define the outer limit of an axis’ range.

To set the outer limit of the x range to 150 you can use xlim(right=150). And to set the outer limit of the y range to 150 you can use ylim(top=150).

And of course you can start a range at other values than 0 as well:

plt.title('Team 1')
plt.xlabel('Height')
plt.ylabel('Weight')

plt.scatter([190,185,185,175,187,179],[80,85,67,75,83,85])

plt.xlim(left=150, right=200)
plt.ylim(bottom=0, top=100)

plt.show()
2 Scatterplots with matplotlib that show the same data, but one has custom limits set with xlim and ylim. Source: own images.

5 Add horizontal or vertical lines to charts

Sometimes a horizontal or vertical line can help make a clear distinction.

Think about when you have a bar chart with both negative and positive values. By adding a horizontal line at y=0 you get a clear distinction between the negative and positive values and it looks better in my opinion.

A horizontal line can be made with the axhline function by passing it a y value, you can also pick a color: plt.axhline(y=0, color=’black’)

Vertical lines can be made with axvline in a similar way: plt.axvline(x=2019, color=’black’)

2 matplotlib bar charts with the same data, but one has a horizontal line at 0. Source: own images.

If you use fig, ax = plt.subplots, you would use the axhline and axvline methods like this:

ax.axhline(y=0)
ax.axvline(x=0)

6 Customize the values along the X or Y axis

In matplotlib charts, ticks are the labels that specify values at the X or Y axis. You can completely customize the ticks that you want to show along an axis.

Let’s look at some example code to make a bar chart that shows yearly revenue data:

import matplotlib.pyplot as plt

years = range(2013,2023)
revenues = [55000, 105000, 300000, 927000, 1405000,
            4905000, 4723000, 7000000, 12000000, 15000000]
plt.bar(years,revenues,color='green')

plt.title('Yearly Revenue 2013-2022')
plt.ylabel('Revenue $')
plt.xlabel('Year')

plt.tight_layout()
plt.show()

The code produces this bar chart:

A matplotlib bar chart with default ticks. Source: own image.

As you can see not every year has gotten a tick on the x axis. And on the y axis we can see scientific notation numbers at intervals of 2 million dollars.

To specify at which values you want a tick to appear you can use the xticks and yticks functions to which you pass a collection of values like a list or range.

To show each year we use the range years, by default ranges have a stepsize of 1. This means that every year will get a tick on the x-axis.

And to show a y-axis tick at each interval of $1 million, we make a new range y_tick_values. The range goes from 0 to 16 million with a stepsize of 1 million: y_tick_values = range(0,16000000,1000000)

Here is the same bar chart making code after the xticks and yticks update:

import matplotlib.pyplot as plt

years = range(2013,2023)
revenues = [55000, 105000, 300000, 927000, 1405000,
            4905000, 4723000, 7000000, 12000000, 15000000]
plt.bar(years,revenues,color='green')

plt.xticks(years)

y_tick_values = range(0,16000000,1000000)
plt.yticks(y_tick_values)

plt.title('Yearly Revenue 2013-2022')
plt.ylabel('Revenue $')
plt.xlabel('Year')

plt.tight_layout()
plt.show()

And here is the new bar chart:

A matplotlib bar chart with customized ticks. Source: own image.

Okay, so now we have ticks at the desired places, but how can we change what is displayed at these ticks? I don’t like the scientific notations along the y axis.

Changing the displayed labels at ticks can easily be done by specifying the desired labels as a second argument in the xticks or yticks function call.

Here is an example:

y_tick_values = range(0,16000000,1000000)
y_tick_labels = ['${:,}'.format(value) for value in y_tick_values]
plt.yticks(y_tick_values,y_tick_labels)

In the code above we make a list y_tick_labels that takes the elements from the y_tick_values range and transforms them.

With '${:,}'.format(value) a value gets transformed into a string that starts with $ and :, is to place commas as thousand separators. So that, for example, 12000000 will be $12,000,000.

Here is the new bar chart code:

import matplotlib.pyplot as plt

years = range(2013,2023)
revenues = [55000, 105000, 300000, 927000, 1405000,
            4905000, 4723000, 7000000, 12000000, 15000000]
plt.bar(years,revenues,color='green')

plt.xticks(years)

y_tick_values = range(0,16000000,1000000)
y_tick_labels = ['${:,}'.format(value) for value in y_tick_values]
plt.yticks(y_tick_values,y_tick_labels)

plt.title('Yearly Revenue 2013-2022')
plt.ylabel('Revenue $')
plt.xlabel('Year')

plt.tight_layout()
plt.show()

Now our bar chart looks like this:

A matplotlib bar chart with default tick values and tick labels. Source: own image.

If you use fig, ax = plt.subplots() than you can use the set_xticks and set_yticks methods like this:

ax.set_xticks(years)
ax.set_yticks(y_tick_values,y_tick_labels)

7 Add a legend to your chart

When you plot multiple series in a chart, adding a legend can help clarify what points belong to what entity.

A legend can be made with the legend function.

When you add the color and label argument to your series, for instance in a scatter plot, the legend function will map the serie’s colors to the labels automatically.

Here is an example:

plt.title('Team 1 vs Team 2')
plt.xlabel('Height')
plt.ylabel('Weight')

plt.scatter([190,185,185,175,187,179],[80,85,67,75,83,85],
             color='red', label='Team 1')
plt.scatter([172,185,180,175,185,179],[70,87,84,79,86,84],
             color='blue', label='Team 2')

plt.legend(loc='upper left')
plt.show()

And here is the chart:

A matplotlib scatterplot with 2 series and a legend. Source: own image.

If you use fig, ax = plt.subplots() than you can use the legend method like this:

ax.legend(loc='upper left')

8 Make charts in dark mode

An easy way to change the background of your chart to black is by using the dark backhtound style. This way the things that were black like texts and lines automatically become white so that they stay visible.

This is how you can select the dark background style:

plt.style.use('dark_background')
2 matplotlib bar charts that are the same but one has a black background and white text and lines because the dark background style was used. Source: own images.

Thank you for reading!

You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:

More charts and graphs in Python:

Data Visualization
Data Science
Matplotlib
Programming
Python
Recommended from ReadMedium