我有以下代码
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread[col sep=comma]{./data/crash_1987.csv}{\loadedtable};
\begin{axis}[
width=20cm,
height=8cm,
grid=both,
enlarge x limits=false,
% no marks,
xticklabels from table={\loadedtable}{Date},
xtick distance={1},
x tick label style={rotate=90},
y tick label style={
/pgf/number format/fixed,
/pgf/number format/precision=5
},
scaled y ticks=false,
legend style={at={(0.025,0.95)},anchor=north west},
ylabel={Value (\$)},
y label style={at={(ticklabel* cs:1.01)}, anchor=south, rotate=-90}
]
\pgfplotsinvokeforeach{SP 500}{
\addplot+[ultra thick] table [x expr=\coordindex, y=#1] {\loadedtable};
\addlegendentry{#1};
}
\end{axis}
\end{tikzpicture}
\end{document}
产生情节,
使用数据
Date,SP 500,DIJA
1987-10-06,319.220001,2548.629883
1987-10-07,318.540009,2551.080078
1987-10-08,314.160004,2516.639893
1987-10-09,311.070007,2482.209961
1987-10-12,309.390015,2471.439941
1987-10-13,314.519989,2508.159912
1987-10-14,305.230011,2412.699951
1987-10-15,298.079987,2355.090088
1987-10-16,282.700012,2246.729980
1987-10-19,224.839996,1738.739990
1987-10-20,236.830002,1841.010010
1987-10-21,258.380005,2027.849976
1987-10-22,248.250000,1950.430054
1987-10-23,248.220001,1950.760010
1987-10-26,227.669998,1793.930054
1987-10-27,233.190002,1846.489990
1987-10-28,233.279999,1846.819946
1987-10-29,244.770004,1938.329956
1987-10-30,251.789993,1993.530029
1987-11-02,255.750000,2014.089966
1987-11-03,250.820007,1963.530029
1987-11-04,248.960007,1945.290039
1987-11-05,254.479996,1985.410034
第一个 x 刻度标签不会显示在图表上。我该如何改变这种情况,让绘图与 (x_{n+1}, y_n) 等坐标不匹配?
答案1
诀窍是添加xtick=data
。然后一切都按预期进行。
\begin{filecontents*}{crash_1987.csv}
Date,SP 500,DIJA
1987-10-06,319.220001,2548.629883
1987-10-07,318.540009,2551.080078
1987-10-08,314.160004,2516.639893
1987-10-09,311.070007,2482.209961
1987-10-12,309.390015,2471.439941
1987-10-13,314.519989,2508.159912
1987-10-14,305.230011,2412.699951
1987-10-15,298.079987,2355.090088
1987-10-16,282.700012,2246.729980
1987-10-19,224.839996,1738.739990
1987-10-20,236.830002,1841.010010
1987-10-21,258.380005,2027.849976
1987-10-22,248.250000,1950.430054
1987-10-23,248.220001,1950.760010
1987-10-26,227.669998,1793.930054
1987-10-27,233.190002,1846.489990
1987-10-28,233.279999,1846.819946
1987-10-29,244.770004,1938.329956
1987-10-30,251.789993,1993.530029
1987-11-02,255.750000,2014.089966
1987-11-03,250.820007,1963.530029
1987-11-04,248.960007,1945.290039
1987-11-05,254.479996,1985.410034
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.3}
\pgfplotstableread[col sep=comma]{crash_1987.csv}{\loadedtable}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=20cm,
height=8cm,
grid=both,
enlarge x limits=false,
xtick=data, % <-- added
xticklabels from table={\loadedtable}{Date},
x tick label style={rotate=90},
legend style={at={(0.025,0.95)},anchor=north west},
ylabel={Value (\$)},
y label style={at={(ticklabel* cs:1.01)}, anchor=south, rotate=-90},
]
\pgfplotsinvokeforeach{SP 500}{
\addplot+ [ultra thick]
table [x expr=\coordindex, y=#1] {\loadedtable};
\addlegendentry{#1};
}
\end{axis}
\end{tikzpicture}
\end{document}