Plotting the Real Estate Data
Plotting the Real Estate Data¶
With some simple plotting, we discover what the relationship between real estate and inflation looks like, as well as one area for improvement within the graphing (the fact that we do have a weird x-axis in this case).
# Plot the data
ax = returns.plot(kind='line')
plt.ylabel("Normalized Index Value")
plt.xlabel("Date")
plt.title("Normalized Indices")
plt.show()
Setting Tick Rotation¶
By calling plt.xticks and passing in a value for rotation, we can change how rotated the tick labels are. This will make it much more readable. Below we see how to rotate to 70 degrees.
# Plot the data
ax = returns.plot(kind='line')
# Add labels
plt.ylabel("Normalized Index Value")
plt.xlabel("Date")
plt.title("Normalized Indices")
# Add rotation
plt.xticks(rotation=70)
plt.show()
Setting Tick Positions¶
Now that we have more space, we can also add more of the labels. For example, maybe we want a label every year. To do that, we first need to find the index positions and then grab every fourth one.
#Now let's find every 4th index
index_positions = list(range(len(returns.index)))
print(index_positions)
print()
print(index_positions[::4])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
[0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76]
Changing Ticks¶
Calling ax.xaxis.set_ticks and passing this in will give you the index positioning at this location. However, we end up noticing some interesting behavior happening where it just filters the indices to be ones that were every fourth index.
# Plot the data
ax = returns.plot(kind='line')
# Add labels
plt.ylabel("Normalized Index Value")
plt.xlabel("Date")
plt.title("Normalized Indices")
# Add rotation
plt.xticks(rotation=70)
# Set ticks
ax.xaxis.set_ticks(index_positions[::4])
plt.show()
What we are going to do now is create the ticks that we want for the labels and feed them in to fit every fourth tick. First we take the index and make a new labeling to it.
To better represent the axis, we are going to make our own custom labels. We want to have one where the form is something like 2000Q1. To do it we map a lambda function on our current index.
# Map a new index
quarter_index = returns.index.map(lambda x: str(x[0]) +"Q"+str(x[1]))
print(quarter_index)
Index(['2000Q1', '2000Q2', '2000Q3', '2000Q4', '2001Q1', '2001Q2', '2001Q3',
'2001Q4', '2002Q1', '2002Q2', '2002Q3', '2002Q4', '2003Q1', '2003Q2',
'2003Q3', '2003Q4', '2004Q1', '2004Q2', '2004Q3', '2004Q4', '2005Q1',
'2005Q2', '2005Q3', '2005Q4', '2006Q1', '2006Q2', '2006Q3', '2006Q4',
'2007Q1', '2007Q2', '2007Q3', '2007Q4', '2008Q1', '2008Q2', '2008Q3',
'2008Q4', '2009Q1', '2009Q2', '2009Q3', '2009Q4', '2010Q1', '2010Q2',
'2010Q3', '2010Q4', '2011Q1', '2011Q2', '2011Q3', '2011Q4', '2012Q1',
'2012Q2', '2012Q3', '2012Q4', '2013Q1', '2013Q2', '2013Q3', '2013Q4',
'2014Q1', '2014Q2', '2014Q3', '2014Q4', '2015Q1', '2015Q2', '2015Q3',
'2015Q4', '2016Q1', '2016Q2', '2016Q3', '2016Q4', '2017Q1', '2017Q2',
'2017Q3', '2017Q4', '2018Q1', '2018Q2', '2018Q3', '2018Q4', '2019Q1',
'2019Q2', '2019Q3', '2019Q4'],
dtype='object')
Then call set_ticklabels passing in the quarter index!
# Plot the data
ax = returns.plot(kind='line')
# Add labels
plt.ylabel("Normalized Index Value")
plt.xlabel("Date")
plt.title("Normalized Indices")
# Add rotation
plt.xticks(rotation=70)
# Set ticks
ax.xaxis.set_ticks(index_positions[::4])
ax.xaxis.set_ticklabels(quarter_index[::4])
plt.show()
Saving our Figure¶
We are done with creating the figure, now we can save it down with the usual plt.savefig!
# Plot the data
ax = returns.plot(kind='line')
# Add labels
plt.ylabel("Normalized Index Value")
plt.xlabel("Date")
plt.title("Normalized Indices")
# Add rotation
plt.xticks(rotation=70)
# Set ticks
ax.xaxis.set_ticks(index_positions[::4])
ax.xaxis.set_ticklabels(quarter_index[::4])
# Save figure
plt.savefig("Images/Real Estate vs. Inflation.png")
plt.close()
We are going to actually make one minor modification and use bbox_inches="tight" as a parameter which will ensure all text and pieces of the figure is fit into the figure that is saved!
# Plot the data
ax = returns.plot(kind='line')
# Add labels
plt.ylabel("Normalized Index Value")
plt.xlabel("Date")
plt.title("Normalized Indices")
# Add rotation
plt.xticks(rotation=70)
# Set ticks
ax.xaxis.set_ticks(index_positions[::4])
ax.xaxis.set_ticklabels(quarter_index[::4])
# Save figure
plt.savefig("Images/Real Estate vs. Inflation.png", bbox_inches="tight")
plt.close()