Reputation: 889
I have data from multiple lines, and I would like to create seaborn lineplot.
each iteration_value has:
I have this code:
def save_graph(properties):
plt.figure()
for iteration_values in properties.iteration_values:
sns_plot = sns.lineplot(iteration_values.x_values, iteration_values.y_values,
hue=iteration_values.line_title)
plt.xlabel = properties.x_label
plt.ylabel = properties.y_label
plt.title(properties.title, fontsize=20)
plt.ylim(0, 1)
figure.savefig(file_path)
plt.close()
iteration_values = [GraphIterationValues([1, 2, 3], [0.1, 0.2, 0.3], "first line title"),
GraphIterationValues(
[1, 2, 3], [0.2, 0.3, 0.4], "second line title"),
GraphIterationValues(
[1, 2, 3], [0.3, 0.4, 0.5], "third line title"),
GraphIterationValues([1, 2, 3], [0.4, 0.5, 0.6], "fourth line title")]
properties = OutputGraphPropertied(
iteration_values, "x label", "y label", "plot title", "./output.jpeg")
save_graph(properties)
But I am getting the error:
ValueError: Could not interpret value `first line title` for parameter `hue`
these are the properties class:
class OutputGraphPropertied:
def __init__(self, graph_iteration_values, x_label, y_label, title, file_path):
self.graph_iteration_values = graph_iteration_values
self.x_label = x_label
self.y_label = y_label
self.title = title
self.file_path = file_path
class GraphIterationValues:
def __init__(self, x_values, y_values, line_title):
self.x_values = x_values
self.y_values = y_values
self.line_title = line_title
I am trying to make it look like this plot with the months (I used this image for ilustration):
Upvotes: 1
Views: 2782
Reputation: 80544
For hue
to work properly, all data should be provided at once. Also, hue
should refer to an array of the same length as x and y, so repeating the line title for each entry in the arrays of x and y values.
Here is a possible adaption of the first few lines of save_graph
:
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
def save_graph(properties):
plt.figure()
x_values = np.concatenate([iteration_values.x_values
for iteration_values in properties.graph_iteration_values])
y_values = np.concatenate([iteration_values.y_values
for iteration_values in properties.graph_iteration_values])
line_titles = np.concatenate([[iteration_values.line_title] * len(iteration_values.x_values)
for iteration_values in properties.graph_iteration_values])
sns_plot = sns.lineplot(x=x_values, y=y_values, hue=line_titles)
...
Another option is to draw multiple plots on the same graph, not using hue
but set the label
:
def save_graph(properties):
plt.figure()
ax = None
for iteration_values in properties.iteration_values:
ax = sns.lineplot(x=iteration_values.x_values, y=iteration_values.y_values,
label=iteration_values.line_title, ax=ax)
...
This will loop through the current color cycle and also create the correct legend.
Upvotes: 2