Let's create a basic line chart of the Tesla stock price and the search popularity and then gradually add more and more styling to our chart.
Plot the Tesla stock price against the Tesla search volume using a line chart and two different axes. Here's what you're aiming for:
.
.
..
...
..
.
.
Solution: Creating a basic chart
This bit should pretty much be review from the previous days' lessons. To create a line plot with two different y-axes we first have to get the current axis and make a copy of it using .twinx()
. Then we can configure each axis separately and call .plot()
.
ax1 = plt.gca() # get current axis ax2 = ax1.twinx() ax1.set_ylabel('TSLA Stock Price') ax2.set_ylabel('Search Trend') ax1.plot(df_tesla.MONTH, df_tesla.TSLA_USD_CLOSE) ax2.plot(df_tesla.MONTH, df_tesla.TSLA_WEB_SEARCH)
Now let's style the chart a bit more. In particular, let's check out the different colours you can use with Matplotlib.
For our updated chart, let's differentiate the two lines and the axis labels using different colours. Try using one of the blue colour names for the search volume and a HEX code for a red colour for the stock price. Here's what you're aiming for:
Hint: you can colour both the axis labels and the lines on the chart using keyword arguments (kwargs).
.
.
..
...
..
.
.
Solution: Adding colours
Your code should now look something like this (with your own choice of colours of course):
ax1 = plt.gca() ax2 = ax1.twinx() ax1.set_ylabel('TSLA Stock Price', color='#E6232E') # can use a HEX code ax2.set_ylabel('Search Trend', color='skyblue') # or a named colour ax1.plot(df_tesla.MONTH, df_tesla.TSLA_USD_CLOSE, color='#E6232E') ax2.plot(df_tesla.MONTH, df_tesla.TSLA_WEB_SEARCH, color='skyblue')
There are still some ways to improve the look of this chart. First off, let's make it larger. Can you make the following changes:
Increase the figure size (e.g., to 14 by 8).
Increase the font sizes for the labels and the ticks on the x-axis to 14.
Rotate the text on the x-axis by 45 degrees.
Add a title that reads 'Tesla Web Search vs Price'
Make the lines on the chart thicker.
Keep the chart looking sharp by changing the dots-per-inch or DPI value.
Set minimum and maximum values for the y and x-axis. Hint: check out methods like set_xlim().
Finally use plt.show() to display the chart below the cell instead of relying on the automatic notebook output.
After the changes your chart should then look something like this:
.
.
..
...
..
.
.
Solution: Additional styling, increasing size & resolution
There's a couple of tweaks to the code going on here. First, we use .figure()
to increase the size and resolution of our chart. Since we now have a bigger chart, we should also increase the font size of our labels and the thickness of our lines.
Finally, we are calling .show()
to explicitly display the chart below the cell. This .show()
method is important to be aware of if you're ever trying to generate charts in PyCharm or elsewhere outside of an interactive notebook like Google Colab or Jupyter. Also, it gives our notebook a very clean look.
# increases size and resolution plt.figure(figsize=(14,8), dpi=120) plt.title('Tesla Web Search vs Price', fontsize=18) ax1 = plt.gca() ax2 = ax1.twinx() # Also, increase fontsize and linewidth for larger charts ax1.set_ylabel('TSLA Stock Price', color='#E6232E', fontsize=14) ax2.set_ylabel('Search Trend', color='skyblue', fontsize=14) ax1.plot(df_tesla.MONTH, df_tesla.TSLA_USD_CLOSE, color='#E6232E', linewidth=3) ax2.plot(df_tesla.MONTH, df_tesla.TSLA_WEB_SEARCH, color='skyblue', linewidth=3) # Displays chart explicitly plt.show()
Here's the code with rotation added to the x-ticks. With .set_ylim()
and .set_xlim()
you have precise control over which data you want to show on the chart. You can either choose hard values like displaying the Tesla stock price between $0 and $600. Or you could use the .min()
and .max()
functions to help you work out the bounds for the chart as well.
plt.figure(figsize=(14,8), dpi=120) plt.title('Tesla Web Search vs Price', fontsize=18) # Increase the size and rotate the labels on the x-axis plt.xticks(fontsize=14, rotation=45) ax1 = plt.gca() ax2 = ax1.twinx() ax1.set_ylabel('TSLA Stock Price', color='#E6232E', fontsize=14) ax2.set_ylabel('Search Trend', color='skyblue', fontsize=14) # Set the minimum and maximum values on the axes ax1.set_ylim([0, 600]) ax1.set_xlim([df_tesla.MONTH.min(), df_tesla.MONTH.max()]) ax1.plot(df_tesla.MONTH, df_tesla.TSLA_USD_CLOSE, color='#E6232E', linewidth=3) ax2.plot(df_tesla.MONTH, df_tesla.TSLA_WEB_SEARCH, color='skyblue', linewidth=3) plt.show()
Fix the Matplotlib Warning (if you see it)
At this point, you might have seen this warning below from Matplotlib:
This is not an error, but an FYI to be explicit about which datetime converter to use. We have a timeline on our x-axis after all. To address this simply follow the instructions in the warning message and add the following code: