Python100dayscourse / part3 /73 - Day 73 - Advanced - Data Visualisation with Matplotlib Programming Languages /007 Data Visualisation with Matplotlib.html
| <p><strong>Matplotlib</strong></p><p>To create our first charts we're going to use a library called <a href="https://matplotlib.org/" rel="noopener noreferrer" target="_blank">Matplotlib</a>. There are many different libraries in Python to help us create charts and graphs. Matplotlib is an incredibly popular one and it works beautifully in combination with Pandas, so let's check it out. </p><p>First, we have to import Matplotlib. </p><pre class="prettyprint linenums">import matplotlib.pyplot as plt</pre><p>Let's do this at the top:</p><figure><img src="https://img-c.udemycdn.com/redactor/raw/2020-09-23_14-56-02-24faa2ac03cb6eb41b5f7135d3ad16bb.png"></figure><p><strong>Mini Challenge</strong></p><p>You can actually show a line chart for the popularity of a programming language using only a single line of code. Can you use the <a href="https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot" rel="noopener noreferrer" target="_blank">.plot() documentation</a> to figure out how to do this? Try and plot the popularity of the Java programming language. Here's what you're aiming for:</p><figure><img height="306" src="https://img-c.udemycdn.com/redactor/raw/2020-09-23_14-58-09-5e4988582be10c8b5172d0164ce466a2.png" width="553"></figure><p><br></p><p><br></p><p><strong>Solution</strong></p><p>All you need to do is supply the values for the horizontal axis (the x-values) and the vertical axis (the y-values) for the chart. The x-values are our dates and the y-values are the number of posts. We can supply these values to the .plot() function by position like so:</p><pre class="prettyprint linenums">plt.plot(reshaped_df.index, reshaped_df.java)</pre><p>or like so if you prefer the square bracket notation. </p><pre class="prettyprint linenums">plt.plot(reshaped_df.index, reshaped_df['java'])</pre><p><br></p><p><strong>Styling the Chart</strong></p><p>Let's look at a couple of methods that will help us style our chart:</p><p><code>.figure()</code> - allows us to resize our chart</p><p><code>.xticks()</code> - configures our x-axis</p><p><code>.yticks()</code> - configures our y-axis</p><p><code>.xlabel()</code> - add text to the x-axis</p><p><code>.ylabel()</code> - add text to the y-axis</p><p><code>.ylim()</code> - allows us to set a lower and upper bound </p><p><br></p><p>To make our chart larger we can provide a width (16) and a height (10) as the <code>figsize</code> of the figure. </p><pre class="prettyprint linenums">plt.figure(figsize=(16,10)) | |
| plt.plot(reshaped_df.index, reshaped_df.java)</pre><p>This will make our chart easier to see. But when we increase the size of the chart, we should also increase the fontsize of the ticks on our axes so that they remain easy to read:</p><figure><img height="505" src="https://img-c.udemycdn.com/redactor/raw/2020-09-23_15-22-03-5e3878721c6ef654b50130ee8825c22e.png" width="728"></figure><p>Now we can add labels. Also, we're never going to get less than 0 posts, so let's set a lower limit of 0 for the y-axis with <code>.ylim()</code>.</p><pre class="prettyprint linenums">plt.xlabel('Date', fontsize=14) | |
| plt.ylabel('Number of Posts', fontsize=14) | |
| plt.ylim(0, 35000)</pre><figure><img src="https://img-c.udemycdn.com/redactor/raw/2020-09-23_15-26-17-522ea65f0dbcd7a8f2d59028698b2cdc.png"></figure><p><br></p><h4>Challenge</h4><p><br></p><p>Now that you've successfully created and styled your chart, can you figure out how to plot both Java and Python next to each other? The result should look something like this:</p><figure><img src="https://img-c.udemycdn.com/redactor/raw/2020-09-23_15-30-24-8f5a861fcfd98b23ac5d62212db135f6.png"></figure> |