Home > database >  axes tickers not formatting correctly
axes tickers not formatting correctly

Time:01-26

What specific changes need to be made to the code below in order for matplotlib to correctly 1.) show each year along the x-axis, and 2.) show meaningful numbers on the y-axis?

THE PROBLEM:

The problem is that the code below causes the resulting matplotlib chart to 1.) give the x-axis tickers as 0 to 140 , and 2.) give the y-axis tickers as 0 to 1.0 1e7.

WHAT WE NEED:

x-axis: The quarterly data needs to be grouped annually along the x-axis. This would mean the tickers show 1985, 1986, 1987,...

y-axis: The magnitude of the data need to be expressed either in all digits on the y-axis or in a format that reduces the number of significant figures to a specific number.

For example, all of the data points have values that are either 7 or 8 digits. Instead of using exponential notation as the current chart does, we need the y-axis tickers to either round to 3 or 4 significant figures, or we need to print the entire 7 or 8 digits at rounded intervals such as 1000000, 1500000, ...

DATA FORMAT:

We have represented the quarters as follows:

q1 = yyyy-03-31
q2 = yyyy-06-30
q3 = yyyy-09-30
q4 = yyyy-12-31

When we run the test code as python try1.py, the print(series.head()) command prints out the first rows as:

C:\path\to>python try1.py
  Time Period    Value
0  1985-03-31  1488501
1  1985-06-30  1518058
2  1985-09-30  1545168
3  1985-12-31  1577771
4  1986-03-31  1600514

CURRENT CODE:

The code in the try1.py file that we are currently using to produce the above UN-desired results is as follows:

from pandas import read_csv
from matplotlib import pyplot

dataFileName = "some.csv"

series = read_csv(dataFileName, sep=',', header=0)
print(series.head())
series.plot()
pyplot.show()

CodePudding user response:

from matplotlib import dates as mdates
import matplotlib.pyplot as plt

df = pd.DataFrame({'date':[datetime.date(1985, 12, 31), datetime.date(1986, 3, 31), datetime.date(1986, 6, 30), datetime.date(1987, 9, 30)],
                   'value':[1488501, 1518058, 1545168, 1577771]})

f, ax = plt.subplots(1,1)
ax.plot(df['date'], df['value'], marker = '.')

# format x axis
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))

# format y axis
ax.ticklabel_format(axis='y', style='plain')

result:

enter image description here

  •  Tags:  
  • Related