坐标轴标记格式不正确
axes tickers not formatting correctly
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?
问题:
问题是下面的代码导致生成的 matplotlib 图表为 1。)将 x 轴标记为 0 到 140+,并且
2.) 将 y 轴标记为 0 到 1.0+ 1e7.
我们需要什么:
x 轴:季度数据需要沿 x 轴每年分组。这意味着行情显示 1985 年、1986 年、1987 年...
y轴:数据的大小需要用y轴上的所有数字表示,或者用将有效数字的数量减少到特定数字的格式表示。
例如,所有数据点的值都是 7 位或 8 位数字。不像当前图表那样使用指数表示法,我们需要将 y 轴代码四舍五入到 3 或 4 位有效数字,或者我们需要打印整个
7 或 8 位数字,以四舍五入的间隔排列,例如 1000000、1500000、...
数据格式:
我们分别代表了以下宿舍:
q1 = yyyy-03-31
q2 = yyyy-06-30
q3 = yyyy-09-30
q4 = yyyy-12-31
当我们运行测试代码为python try1.py
时,print(series.head())
命令打印出第一行为:
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
当前代码:
try1.py
文件中的代码,我们目前用来产生上述 UN-desired 结果如下:
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()
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')
结果:
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?
问题:
问题是下面的代码导致生成的 matplotlib 图表为 1。)将 x 轴标记为 0 到 140+,并且 2.) 将 y 轴标记为 0 到 1.0+ 1e7.
我们需要什么:
x 轴:季度数据需要沿 x 轴每年分组。这意味着行情显示 1985 年、1986 年、1987 年...
y轴:数据的大小需要用y轴上的所有数字表示,或者用将有效数字的数量减少到特定数字的格式表示。
例如,所有数据点的值都是 7 位或 8 位数字。不像当前图表那样使用指数表示法,我们需要将 y 轴代码四舍五入到 3 或 4 位有效数字,或者我们需要打印整个 7 或 8 位数字,以四舍五入的间隔排列,例如 1000000、1500000、...
数据格式:
我们分别代表了以下宿舍:
q1 = yyyy-03-31
q2 = yyyy-06-30
q3 = yyyy-09-30
q4 = yyyy-12-31
当我们运行测试代码为python try1.py
时,print(series.head())
命令打印出第一行为:
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
当前代码:
try1.py
文件中的代码,我们目前用来产生上述 UN-desired 结果如下:
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()
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')
结果: