Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PASE
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
DEAL public
PASE
Commits
3ea5a7f5
Commit
3ea5a7f5
authored
3 months ago
by
Bouvry Arnaud
Browse files
Options
Downloads
Plain Diff
Merge branch '98-feature-generic-time-series-plots-for-crop-and-soil' into 'develop'
[feature] generic time series plot See merge request
!13
parents
b45e9958
504d36cc
No related branches found
No related tags found
1 merge request
!21
v1.1.0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
MODULES/DATA_MANAGEMENT/plots/generic_time_series.py
+76
-0
76 additions, 0 deletions
MODULES/DATA_MANAGEMENT/plots/generic_time_series.py
with
76 additions
and
0 deletions
MODULES/DATA_MANAGEMENT/plots/generic_time_series.py
0 → 100644
+
76
−
0
View file @
3ea5a7f5
import
matplotlib.pyplot
as
plt
import
matplotlib.dates
as
mdates
import
numpy
as
np
import
datetime
def
plot_variables
(
nyears_data_list
,
variables
,
start_date
,
end_date
):
"""
This function is provided as an example and a quick way to plot time series data
for mean multiple spatialized variables from multiple datasets within a specified date range.
:param nyears_data_list: A list of dictionaries, each containing yearly data dictionaries.
:type nyears_Data_list: list
:param variables: A list of variable names to be plotted.
:type variables: list
:param start_date: The plot start date in the format
"
YYYY-MM-DD
"
:type start_date: str
:param start_date: The plot end date in the format
"
YYYY-MM-DD
"
:type end_date: str
"""
start_dt
=
datetime
.
datetime
.
strptime
(
start_date
,
"
%Y-%m-%d
"
)
end_dt
=
datetime
.
datetime
.
strptime
(
end_date
,
"
%Y-%m-%d
"
)
timedelta
=
end_dt
-
start_dt
num_vars
=
len
(
variables
)
fig
,
axes
=
plt
.
subplots
(
num_vars
,
1
,
figsize
=
(
12
,
6
*
num_vars
),
sharex
=
False
)
if
num_vars
==
1
:
axes
=
[
axes
]
for
ax
,
variable
in
zip
(
axes
,
variables
):
dates
=
[]
values
=
[]
for
i
,
nyears_data
in
enumerate
(
nyears_data_list
):
for
year
,
year_dict
in
nyears_data
.
items
():
if
variable
in
year_dict
:
print
(
f
'
{
variable
}
found in dict
{
i
}
, year
{
year
}
'
)
var_dict
=
year_dict
[
variable
]
for
date_str
,
value
in
var_dict
.
items
():
current_dt
=
datetime
.
datetime
.
strptime
(
str
(
date_str
),
"
%Y-%m-%d %H:%M:%S
"
)
if
start_dt
<=
current_dt
<=
end_dt
:
dates
.
append
(
current_dt
)
values
.
append
(
np
.
mean
(
value
))
if
not
dates
:
ax
.
set_title
(
f
"
No data found for
{
variable
}
"
)
continue
values
=
np
.
array
(
values
)
# Convert to NumPy array in case of multiple dimensions
if
values
.
ndim
==
1
:
ax
.
plot
(
dates
,
values
,
label
=
variable
,
linewidth
=
'
2
'
)
else
:
for
i
in
range
(
values
.
shape
[
1
]):
ax
.
plot
(
dates
,
values
[:,
i
],
label
=
f
"
{
variable
}
_
{
i
}
"
)
ax
.
xaxis
.
set_major_locator
(
mdates
.
YearLocator
())
ax
.
xaxis
.
set_major_formatter
(
mdates
.
DateFormatter
(
'
%Y
'
))
if
timedelta
.
total_seconds
()
<=
63072000
:
ax
.
xaxis
.
set_minor_locator
(
mdates
.
MonthLocator
())
ax
.
xaxis
.
set_minor_formatter
(
mdates
.
DateFormatter
(
'
%m
'
))
ax
.
set_ylabel
(
"
Value
"
)
ax
.
set_title
(
f
"
{
variable
}
"
)
ax
.
legend
()
ax
.
grid
(
True
)
ax
.
tick_params
(
axis
=
'
x
'
,
labelrotation
=
45
)
plt
.
xlabel
(
"
Date
"
)
plt
.
xticks
(
rotation
=
45
)
plt
.
show
()
# Example usage:
# plot_variables([Soil_plot.nyears_data, Crop_plot[0].nyears_data, Crop_plot[1].nyears_data], ['Norg', 'exported_BM'], '2008-01-01', '2009-12-31')
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment