簡體   English   中英

使用多條線和色調創建 seaborn 線圖

[英]create seaborn lineplot with multiple lines and hue

我有來自多行的數據,我想創建 seaborn 線圖。

每個迭代值都有:

  • x_values- 浮點數列表
  • y_values- 介於 0 和 1 之間的列表
  • line_title - 一個色調字符串。

我有這個代碼:

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)

但我收到錯誤:

ValueError: Could not interpret value `first line title` for parameter `hue`

這些是 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

我試圖讓它看起來像這個 plot 幾個月(我用這張圖片來說明): 在此處輸入圖像描述

為了使hue正常工作,應立即提供所有數據。 此外, hue應該引用與 x 和 y 長度相同的數組,因此為 x 和 y 值的 arrays 中的每個條目重復行標題。

這是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)
    ...

生成的線圖,每行帶有色調

另一種選擇是在同一張圖上繪制多個圖,不使用hue ,而是設置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)
    ...

這將循環當前顏色循環並創建正確的圖例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM