我想绘制一个图表,在横轴上带有日期和时间,在纵轴上带有整数值。
我拥有的数据为.csv格式,请参见file。
我尝试过
db = Dataset[
Flatten[Import[
"C:\\Users\\Matteo\\AppData\\Roaming\\MetaQuotes\\Terminal\\\
C10F84FF203255BEE7679EDC837848E1\\MQL4\\Files\\Data\\date_box.csv"]]]
DateListPlot[db]
但是结果是错误:
"The first two levels of \
{$Failed,$Failed,$Failed,$Failed,$Failed,$Failed,$Failed,$Failed,$\<<9951>>} cannot be transposed. "
Unable to automatically determine horizontal coordinates for the \
given data and DataRange.
"\!\(TraditionalForm\`{\"2018.02.02 21:59;0\", \"2018.02.02 \
21:59;0\", \"2018.02.02 21:58;0\", ...\) is not a \
valid dataset or list of datasets."
我无法解决
最佳答案
data = Import["C:\\Users\\Matteo\\...\\Files\\Data\\date_box.csv"];
d2 = StringSplit[#, ";"] & /@ Flatten[data];
t = DateList /@ d2[[All, 1]];
i = ToExpression /@ Last /@ d2;
DateListPlot[Transpose[{t, i}]]
删除周末
days = Take[#, 3] & /@ t;
union = {#, DateString[#, "DayNameShort"]} & /@ Union[days]
看起来您星期天有一些数据点
sundaypos = Position[days, {2018, 1, 28}];
Extract[i, sundaypos]
删除它们
tnew = Delete[t, sundaypos];
inew = Delete[i, sundaypos];
通过调整来消除周末
t1 = AbsoluteTime[{2018, 1, 24}];
t2 = AbsoluteTime[{2018, 1, 27}];
t3 = AbsoluteTime[{2018, 1, 29}];
a = AbsoluteTime /@ tnew;
a2 = If[# >= t2, # - (t3 - t2), #] & /@ a;
labels = {AbsoluteTime[First[#]], Last[#]} & /@
DeleteCases[union, {{2018, 1, 28}, "Sun"}];
labels[[All, 1]] = If[# >= t2, # - (t3 - t2), #] & /@
labels[[All, 1]];
ListLinePlot[Sort@Transpose[{a2, inew}], Frame -> True,
FrameTicks -> {{Automatic, None}, {labels, None}}]
版本3
{t1, t2, t3, t4, t5} = AbsoluteTime /@ {{2018, 1, 24},
{2018, 1, 26, 22, 0, 0}, {2018, 1, 28, 22, 0, 0},
{2018, 2, 2, 22, 0, 0}, {2018, 2, 4, 22, 0, 0}};
a = AbsoluteTime /@ t;
a2 = Which[
# >= t4, # - (t3 - t2) - (t5 - t4),
# >= t2, # - (t3 - t2),
True, #] & /@ a;
labels = {AbsoluteTime[#],
DateString[#, {"MonthNameShort", " ", "Day", "\n00:00"}]} & /@
Append[DeleteCases[union,
{{2018, 1, 28}, "Sun"}], {{2018, 2, 5}, "Mon"}][[All, 1]];
labels[[All, 1]] = Which[
# >= t4, # - (t3 - t2) - (t5 - t4),
# >= t2, # - (t3 - t2),
True, #] & /@ labels[[All, 1]];
ListPlot[Sort@Transpose[{a2, i}], Frame -> True,
FrameTicks -> {{Automatic, None}, {labels, None}},
Epilog -> {Dashed, Gray, Line[{{t2, 100}, {t2, -800}}],
Line[{{t4 - (t3 - t2), 100}, {t4 - (t3 - t2), -800}}]},
ImageSize -> Large]