Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added normal plot for time series #550

Merged
merged 28 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
760244e
added normal plot for time series
Gerhardsa0 Feb 21, 2024
2b92135
added normal plot for time series
Gerhardsa0 Feb 21, 2024
cb710ca
style: apply automated linter fixes
megalinter-bot Feb 21, 2024
daf7709
-added labels
Gerhardsa0 Feb 27, 2024
92d1c24
style: apply automated linter fixes
megalinter-bot Feb 27, 2024
3480a15
- scatter and line plot functions
Gerhardsa0 Feb 27, 2024
3060bdb
linter fixes
Gerhardsa0 Feb 27, 2024
ae27d10
linter fixes
Gerhardsa0 Feb 27, 2024
8dd64ad
style: apply automated linter fixes
megalinter-bot Feb 27, 2024
f44d3e4
- added 2 parameters and the same function schema as in the table class
Gerhardsa0 Feb 27, 2024
881fc59
Merge remote-tracking branch 'origin/549-feat-normal-visualization-of…
Gerhardsa0 Feb 27, 2024
ef1c11c
linter fixes
Gerhardsa0 Feb 27, 2024
177c52e
linter fixes
Gerhardsa0 Feb 27, 2024
5e36a78
style: apply automated linter fixes
megalinter-bot Feb 27, 2024
7437360
fixed UnknownError
Gerhardsa0 Feb 27, 2024
e85b887
Merge remote-tracking branch 'origin/549-feat-normal-visualization-of…
Gerhardsa0 Feb 27, 2024
3e08d06
Revert "fixed UnknownError"
Gerhardsa0 Feb 27, 2024
7778e5c
Revert "Revert "fixed UnknownError""
Gerhardsa0 Feb 27, 2024
ca724f2
fixed
Gerhardsa0 Feb 27, 2024
1710734
fixed
Gerhardsa0 Feb 27, 2024
6a73273
style: apply automated linter fixes
megalinter-bot Feb 27, 2024
f247a99
deleted useless catch
Gerhardsa0 Feb 27, 2024
38c5d84
style: apply automated linter fixes
megalinter-bot Feb 28, 2024
97bb01c
Merge branch 'main' into 549-feat-normal-visualization-of-time-series
Gerhardsa0 Mar 1, 2024
779d5fd
applied code changes
Gerhardsa0 Mar 4, 2024
9afba91
fixed comment issue
Gerhardsa0 Mar 4, 2024
b8d966c
fixed comment issue
Gerhardsa0 Mar 4, 2024
ba11d24
Merge branch 'main' into 549-feat-normal-visualization-of-time-series
lars-reimann Mar 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 149 additions & 1 deletion src/safeds/data/tabular/containers/_time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

from safeds.data.image.containers import Image
from safeds.data.tabular.containers import Column, Row, Table, TaggedTable
Expand Down Expand Up @@ -36,7 +37,7 @@ def _from_tagged_table(

Parameters
----------
table : TaggedTable
tagged_table: TaggedTable
The tagged table.
time_name: str
Name of the time column.
Expand Down Expand Up @@ -906,3 +907,150 @@ def plot_lagplot(self, lag: int) -> Image:
plt.close() # Prevents the figure from being displayed directly
buffer.seek(0)
return Image.from_bytes(buffer.read())

def plot_lineplot(self, x_column_name: str | None = None, y_column_name: str | None = None) -> Image:
"""

Plot the time series target or the given column(s) as line plot.

The function will take the time column as the default value for y_column_name and the target column as the
default value for x_column_name.

Parameters
----------
x_column_name:
lars-reimann marked this conversation as resolved.
Show resolved Hide resolved
The column name of the column to be plotted on the x-Axis, default is the time column.
y_column_name:
The column name of the column to be plotted on the y-Axis, default is the target column.

Returns
-------
plot:
The plot as an image.

Raises
------
NonNumericColumnError
If the time series given columns contain non-numerical values.

UnknownColumnNameError
If one of the given names does not exist in the table

Examples
--------
>>> from safeds.data.tabular.containers import TimeSeries
>>> table = TimeSeries({"time":[1, 2], "target": [3, 4], "feature":[2,2]}, target_name= "target", time_name="time", feature_names=["feature"], )
>>> image = table.plot_lineplot()

"""
self._data.index.name = "index"
if x_column_name is not None and not self.get_column(x_column_name).type.is_numeric():
raise NonNumericColumnError("The time series plotted column contains non-numerical columns.")

if y_column_name is None:
y_column_name = self.target.name

elif y_column_name not in self._data.columns:
raise UnknownColumnNameError([y_column_name])

if x_column_name is None:
x_column_name = self.time.name

if not self.get_column(y_column_name).type.is_numeric():
raise NonNumericColumnError("The time series plotted column contains non-numerical columns.")

fig = plt.figure()
ax = sns.lineplot(
data=self._data,
x=x_column_name,
y=y_column_name,
)
ax.set(xlabel=x_column_name, ylabel=y_column_name)
ax.set_xticks(ax.get_xticks())
ax.set_xticklabels(
ax.get_xticklabels(),
rotation=45,
horizontalalignment="right",
) # rotate the labels of the x Axis to prevent the chance of overlapping of the labels
plt.tight_layout()

buffer = io.BytesIO()
fig.savefig(buffer, format="png")
plt.close() # Prevents the figure from being displayed directly
buffer.seek(0)
self._data = self._data.reset_index()
return Image.from_bytes(buffer.read())

def plot_scatterplot(
self,
x_column_name: str | None = None,
y_column_name: str | None = None,
) -> Image:
"""
Plot the time series target or the given column(s) as scatter plot.

The function will take the time column as the default value for x_column_name and the target column as the
default value for y_column_name.

Parameters
----------
x_column_name:
The column name of the column to be plotted on the x-Axis.
y_column_name:
The column name of the column to be plotted on the y-Axis.

Returns
-------
plot:
The plot as an image.

Raises
------
NonNumericColumnError
If the time series given columns contain non-numerical values.

UnknownColumnNameError
If one of the given names does not exist in the table

Examples
--------
>>> from safeds.data.tabular.containers import TimeSeries
>>> table = TimeSeries({"time":[1, 2], "target": [3, 4], "feature":[2,2]}, target_name= "target", time_name="time", feature_names=["feature"], )
>>> image = table.plot_scatterplot()

"""
self._data.index.name = "index"
if x_column_name is not None and not self.get_column(x_column_name).type.is_numeric():
raise NonNumericColumnError("The time series plotted column contains non-numerical columns.")

if y_column_name is None:
y_column_name = self.target.name
elif y_column_name not in self._data.columns:
raise UnknownColumnNameError([y_column_name])
if x_column_name is None:
x_column_name = self.time.name

if not self.get_column(y_column_name).type.is_numeric():
raise NonNumericColumnError("The time series plotted column contains non-numerical columns.")

fig = plt.figure()
ax = sns.scatterplot(
data=self._data,
x=x_column_name,
y=y_column_name,
)
ax.set(xlabel=x_column_name, ylabel=y_column_name)
ax.set_xticks(ax.get_xticks())
ax.set_xticklabels(
ax.get_xticklabels(),
rotation=45,
horizontalalignment="right",
) # rotate the labels of the x Axis to prevent the chance of overlapping of the labels
plt.tight_layout()

buffer = io.BytesIO()
fig.savefig(buffer, format="png")
plt.close() # Prevents the figure from being displayed directly
buffer.seek(0)
self._data = self._data.reset_index()
return Image.from_bytes(buffer.read())
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading