-1

Trying to have my y axis range from 0-450,000 with an increment value of 50000. I believe I have the right technique incorporated with "plt.yticks(np.arange(0,450001,50000))" Confused as to why all my y axis values disappear however when I run it. I have also tried "ax = plt.gca() ax.set_ylim([0,450000])" The numbers just end up looking smudged on the bottom of y axis. Here is my code thus far...

import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import rcParams
import numpy as np
%matplotlib inline

rcParams['figure.figsize'] = 20,10
df = pd.read_csv('https://raw.githubusercontent.com/ObiP1/The-Future-Value-of-Homes/master/AverageHomeValues.csv')

plt.title('Median Cost Of Maryland Homes', fontsize=30)
plt.ylabel('Median Price Of Home',fontsize=25)

plt.yticks(np.arange(0,450001,50000))

plt.xlabel('Year', fontsize=25)
plt.plot(df.YEAR, df.MED_COST)
plt.grid(True)
ObiP1
  • 1
  • 2

2 Answers2

1

The problem is that your MED_COST column are strings, not numbers. These strings get used as ticklabels, but for tick positions at 0,1,2,3,4,5,... Setting tick positions at 0, 50000, ... will make everything invisible, except tick 0.

So, converting these strings to numbers should solve the issues. They can be shown as currencies via the StrMethodFormatter. Instead of setting the ticks explicitly, MultipleLocator(50000) is another option to prevent that the ticks should be recalculated when new data comes available.

As plot can change some of the settings, first calling plot and only afterwards setting labels and ticks can be helpful.

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import rcParams
import numpy as np
from matplotlib import ticker

rcParams['figure.figsize'] = 20, 10

df = pd.DataFrame({
    'YEAR': [1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010, 2020],
    'MED_COST': ['$31500', '$48700', '$58600', '$71800', '$115400', '$148800', '$146000', '$250242', '$295000']})
# make the 'MED_COST' column numeric
df.MED_COST = [int(m[1:]) for m in df.MED_COST]

plt.plot(df.YEAR, df.MED_COST)
plt.title('Median Cost Of Maryland Homes', fontsize=30)
plt.ylabel('Median Price Of Home', fontsize=25)

plt.xlabel('Year', fontsize=25)
plt.yticks(np.arange(0, 450001, 50000))
# plt.gca().yaxis.set_major_locator(ticker.MultipleLocator(50000))
plt.gca().yaxis.set_major_formatter(ticker.StrMethodFormatter('${x:,.0f}'))

plt.grid(True)
plt.show()

resulting plot

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Our answers started out approximately the same, I think, but with your edits, this is better (eg, StrMethodFormatter, etc). – tom10 Apr 10 '20 at 01:51
  • @ObiP1 Was this answer helpful? – JohanC Apr 11 '20 at 22:27
  • @ObiP1: since you are new here, I'll point out that it would be helpful if you would accept this answer. [This post explains how and why.](https://meta.stackoverflow.com/questions/251078/how-to-update-and-accept-answers) If you have a reason not to, that's fine too, but at this point, it would be useful to explain why, since it seems to be a good answer to your question. – tom10 Apr 15 '20 at 00:13
1

The problem is that the $ strings are not interpreted as values but as strings (that line looked much to straight, didn't it?). If you convert it (as here) you get this:

enter image description here

df = pd.read_csv('https://raw.githubusercontent.com/ObiP1/The-Future-Value-of-Homes/master/AverageHomeValues.csv')
df[df.columns[1:]] = df[df.columns[1:]].replace('[\$,]', '', regex=True).astype(float)

plt.title('Median Cost Of Maryland Homes', fontsize=30)
plt.ylabel('Median Price Of Home',fontsize=25)

plt.yticks(np.arange(0,450001,50000))

plt.xlabel('Year', fontsize=25)
plt.plot(df.YEAR, df.MED_COST, 'o')
plt.grid(True)
tom10
  • 67,082
  • 10
  • 127
  • 137