누적 시계열도
다른 단위로 측정 된 여러 시계열이 있습니다. 공통 x 축이있는 내 데이터의 누적 플롯을 갖고 싶습니다. 여기에 예가 있습니다 . 설명을 위해 여기 내 데이터가 있습니다.
nobs = 100;
TS1= RandomVariate[NormalDistribution[0, 1], nobs];
TS2= Accumulate[RandomVariate[NormalDistribution[0, 5], nobs]];
TS3= RandomInteger[{-10, 100}, nobs];
dates = DateRange[DatePlus[Today, Quantity[-nobs + 1, "Days"]], Today];
mytsdata = TimeSeries[#, {dates}] & /@ {TS1, TS2, TS3};
mergeddata = TimeSeriesThread[# &, mytsdata];
내가 사용할 수 있습니다 DateListPlot또는 StackedDateListPlot내 데이터 세트 플롯 mytsdata또는 mergeddata그러나 이러한 플롯 중 어느 것도 내 관심을 제공합니다. MATLAB을 사용하여 그래프를 만들었는데 다음과 같습니다.
Ma 코드를 사용하여 유사한 그래프를 만들 수 있습니까? 그래프 속성을 변경하는 유연성은 많은 가치를 추가합니다. 주요 요구 사항은 x 축이 하나만 있어야한다는 것입니다. 시계열은 길이가 다를 수 있습니다.
업데이트 : 다음은 길이가 다른 여러 시계열 목록입니다.
nobs = 100;
dates = DateRange[DatePlus[Today, Quantity[-nobs + 1, "Days"]], Today];
s1 = RandomVariate[NormalDistribution[0, 1], nobs];
s1ts = TimeSeries[s1, {dates}];
s2 = Accumulate[RandomVariate[NormalDistribution[0, 5], nobs - 25]];
s2ts = TimeSeries[s2, {dates[[26 ;;]]}];
s3 = RandomInteger[{-10, 100}, nobs - 50];
s3ts = TimeSeries[s3, {dates[[51 ;;]]}];
mergeddata2 =
TimeSeriesThread[# &, {s1ts, s2ts, s3ts},
ResamplingMethod -> Missing[]];
mergeddata2다음과 같이 플롯하고 싶습니다 .
업데이트 2 :
Rohit의 제안을 반영하여 다음 코드로 그래프를 생성 할 수있었습니다.
drange = {mergeddata2["Dates"][[1]], mergeddata2["Dates"][[-1]]};
ResourceFunction["PlotGrid"][{
{DateListPlot[s1ts, Frame -> True,
PlotRange -> {drange, Automatic}]},
{DateListPlot[s2ts, Frame -> True,
PlotRange -> {drange, Automatic}]},
{DateListPlot[s3ts, Frame -> True, PlotRange -> {drange, Automatic}]}
}
]
추가 개선을위한 모든 제안을 환영합니다.
답변
리소스 함수 PlotGrid 에는 레이아웃과 축을 제어하기위한 많은 옵션이 있습니다.
ResourceFunction["PlotGrid"][mytsdata // Map[DateListPlot /* List]]
이것은 당신이 원하는 것을 대부분 얻을 수 있습니다.
즉, 현재 솔루션에 도달하기 위해이 게시물을 몇 번 편집했습니다.
Grid[{
{"TS1",DateListPlot[mytsdata[[1]], ImageSize -> 350, AspectRatio -> 1/2, ImagePadding -> {{25, 1}, {0, 0}}]},
{"TS2", DateListPlot[mytsdata[[2]], ImageSize -> 350, AspectRatio -> 1/2, ImagePadding -> {{25, 1}, {0, 0}}]},
{"TS3", DateListPlot[mytsdata[[3]], ImageSize -> 350, AspectRatio -> 1/2, ImagePadding -> {{25, 1}, {15, 0}}]},
{"", "TIME 2020"}
},
Alignment -> {Right, Left}]
몇 가지 설명이 이어집니다 ...
적어도 내가 아는 한 Mathematica는 원하는 것을 간단하게 수행 할 수 없습니다.
내 솔루션은 Grid원하는 그래프를 복제하는 데 사용 됩니다.
보시다시피, Grid3 DateListPlot초가 있습니다.
ImagePadding플롯의 왼쪽 세로 축을 정렬하는 데 사용 합니다. 이 작업을 수행하는 자동 방법을 찾지 못했습니다. 다른 사람이 제안을 할 수도 있습니다.
또한 ImagePadding위의 플롯 위에 연속적인 각 플롯을 겹쳐 두 개의 상위 플롯에 대한 하단 월 레이블을 숨기는 데 사용합니다. 이를 수행하는 다른 방법이 있습니다. 다른 답변이 무엇을 가져 오는지 봅시다.
또한 .NET Framework의 맨 아래 행에 TIME & 2020을 추가했습니다 Grid.
TimeSeries객체에 고유 한 박자표 가 있는지 확인하기 위해 무작위 간격 의 날짜 범위 를 만드는 몇 가지 함수를 작성했습니다 . 이러한 범위 Today는 임의의 일 수 를 플러스 또는 마이너스로 시작 하여 주어진 단계 수만큼 시간을 거슬러 올라갑니다 .
또한 위에서 설명한대로 여러 임의의 날짜 범위에 대한 공통 범위를 반환하는 함수를 제공합니다 .
마지막으로 코드 섹션에는 여러 값 목록과 해당 날짜 목록을 TimeSeries개체 로 구성하는 함수가 있습니다.
Clear[randf, randomDate, aroundToday, randomDates]
(* Returns an integer between 3 and 7 *)
randf = (RandomInteger[{3, 7}, #] &) /* First;
(* Returns a date that is a random number of days before the input date *)
randomDate[date_, random_ : randf, unit_ : "Days"] := DatePlus[date, {-random[1], unit}];
(* Returns a random number of days before of after Today's date *)
aroundToday[random_ : randf, unit_ : "Days"] := DatePlus[Today, {RandomChoice[{-1, 1}] random[1], unit}];
(* Returns n randomly generated days starting from around Today and going back in random number of steps *)
randomDates[n_, random_ : randf, unit_ : "Days"] := With[{r = randomDate[#, random, unit] &},
NestList[r, aroundToday[random, unit], n] // Reverse
]
(* Accepts lists of dates and returns their common range *)
dateRange[dates__] := Map[Through[{Min, Max}[#]] &, {dates}] // Transpose /* (
MapThread[Construct, {{Min, Max}, #}] &)
(* Composes TimeSeries objects from a list of date lists and a list of value lists *)
(* A working assumption is that corresponding dates and values sublists are of the same Length ns[i]] *)
(* The returned TimeSeries have a random number of the original entries removed *)
makeTimeSeries[dates_, values_, ns_, random_ : randf] := MapThread[
With[{t = #1, y = #2, is = RandomInteger[{1, #3}, randf[1]]},
TimeSeries[#2, {#1}] & @@ Transpose[ReplacePart[Transpose[{t, y}], is -> Nothing // Thread]]
] &, {dates, values, ns}]
위의 코드를 사용 하여 OP 편집에서 길이가 다른 데이터 인에 제공된 값에 대해 실제로 균일하지 않은 간격의 날짜를 생성 할 수 있습니다 {s1, s2, s3}.
(* Obtained required data lengths *)
ns = Length /@ {s1, s2, s3};
(* Generate randomly spaced dates, starting from Today and extending back into the past *)
dates = Table[randomDates[n - 1], {n, ns}];
(* Finally, compose the corresponding TimeSeries objects *)
mytsdata = makeTimeSeries[dates, {s1, s2, s3}, ns]
(* Record the common range of the various date lists *)
rng = dateRange @@ ((#["Dates"] &) /@ mytsdata)
내 시스템에서 위의 코드에 대한 하나의 평가가 생성되었습니다.
모든 시계열의 범위와 관측치 수가 다른 점에 유의하십시오.
이제 OP에 대한 답변을 제공하기 위해 다음 기능을 사용했습니다.
ClearAll[manyPlots]
Options[manyPlots] = {"Plot1" -> None, "Plot2" -> None, "Plot3" -> None};
manyPlots[ts_, opts : OptionsPattern[manyPlots]] := Module[{opts1, opts2, opts3, allOpts},
allOpts = {opts1, opts2, opts3} = OptionValue[{"Plot1", "Plot2", "Plot3"}];
MapThread[DateListPlot[#1, Apply[Sequence, #2]] &, {ts, allOpts}] // List /* Transpose /* GraphicsGrid
]
이 manyPlots기능을 통해 사용자는 다양한 플롯에 다양한 옵션을 전달할 수 있습니다. 예 :
manyPlots[mytsdata,
"Plot1" -> {PlotLabel -> "a", PlotRange -> {rng, Automatic}},
"Plot2" -> {PlotLabel -> "b", PlotRange -> {rng, Automatic}, PlotStyle -> ColorData[97, "ColorList"][[2]]},
"Plot3" -> {PlotLabel -> "c", PlotRange -> {rng, Automatic}, PlotStyle -> ColorData[97, "ColorList"][[3]]}]
플롯에 다른 레이블을 제공하고 공통 범위에 모두 표시되도록합니다. 또한 PlotStyle두 번째 및 세 번째 플롯 의을 수정합니다 .
이 접근법을 사용하여 수용 할 수있는 다른 많은 변화가 있다고 생각합니다.