dangvinh77's picture
Upload folder using huggingface_hub
413a032 verified
<p><strong>Solution:&nbsp;Two Line Charts Next to Each Other</strong></p><p>The trick is simply calling the .plot() method twice. That's all there is to it! =)</p><pre class="prettyprint linenums">plt.figure(figsize=(16,10)) # make chart larger
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.xlabel('Date', fontsize=14)
plt.ylabel('Number of Posts', fontsize=14)
plt.ylim(0, 35000)
plt.plot(reshaped_df.index, reshaped_df.java)
plt.plot(reshaped_df.index, reshaped_df.python) # Tadah!</pre><figure><img src="https://img-c.udemycdn.com/redactor/raw/2020-09-23_15-44-51-4c189e51e7727e1268d892b1dd118d9b.png"></figure><p>But what if we wanted to plot all the programming languages on the same chart? We don't want to type out .plot() a million times, right?&nbsp;We can actually just use a for-loop:</p><pre class="prettyprint linenums">for column in reshaped_df.columns:
plt.plot(reshaped_df.index, reshaped_df[column])</pre><p>This will allow us to iterate over each column in the DataFrame and plot it on our chart. The final result should look like this:</p><figure><img src="https://img-c.udemycdn.com/redactor/raw/2020-09-23_15-44-25-e5665da7efcc5de7dca53f83f617ec15.png"></figure><p>But wait, which language is which?&nbsp;It's really hard to make out without a legend that tells us which colour corresponds to each language. Let's modify the plotting code to add a label for each line based on the column name (and make the lines thicker at the same time using linewidth). Then let's add a legend to our chart: </p><pre class="prettyprint linenums">plt.figure(figsize=(16,10))
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.xlabel('Date', fontsize=14)
plt.ylabel('Number of Posts', fontsize=14)
plt.ylim(0, 35000)
for column in reshaped_df.columns:
plt.plot(reshaped_df.index, reshaped_df[column],
linewidth=3, label=reshaped_df[column].name)
plt.legend(fontsize=16) </pre><p>We should now see something like this:</p><figure><img src="https://img-c.udemycdn.com/redactor/raw/2020-09-23_16-23-20-177e4894db9a12e6c6369f2beaed4ccd.png"></figure><p>Looks like Python is the most popular programming language judging by the number of posts on Stack Overflow! Python for the win! =)&nbsp;</p><p><br></p>