Jak dodać do tego legendę?

0
# Show correlation between sepal_lenth and petal_width with size of sepal_width by classon plot
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.cm as cm

def load_iris():
    iris = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data")
    iris.columns = ["sepal_length", "sepal_width", "petal_length", "petal_width", "classs"]
    return iris

"""
@author: Pyxis
"""
def _map_factor_level(x):
    return list(x.factorize())[0]


def plot_iris_correlations(iris):
    fig, ax = plt.subplots()

    colormap = cm.Accent
    colorlist = colormap(_map_factor_level(iris.classs))

    ax.scatter(iris.sepal_length, iris.petal_length, s=iris.sepal_width*10, c=colorlist)

    plt.show()

def main():
    iris = load_iris()
    plot_iris_correlations(iris)

main()

Na legendzie powinny być 3 kolory i 3 labele.
nie mam pomysłu jak można ten cholerny ax.legend() zaddżustować. Czyżbym musiał stworzyć po 3 ax na 1 fig?

def plot_iris_correlations(iris):
    fig, ax = plt.subplots()

    colormap = cm.Accent
    colorlist = [colormap(i) for i in _map_factor_level(iris.classs)]

    for i,k in enumerate(iris.classs.unique()):
        _iris = iris[iris.classs == k]
        ax.scatter(_iris.sepal_length, _iris.petal_length, s=_iris.sepal_width*10, c=colormap(i))
    ax.legend(iris.classs.unique())
    
    plt.show()

ale wtedy dostaję warningi o tym, że nie należy podawać 1 wartości do argumentu c

c' argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with 'x' & 'y'. Please use a 2-D array with a single row if you really want to specify the same RGB or RGBA value for all points.

Tak ma wyglądać:
title

0

W sumie dokumentacja na to odpowiada, podaje możliwości... I babol nie siedzi w legend, tylko nieodpowiednim przygotowaniu dla legend :)
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.legend.html

A tu przykład jak powinieneś użyć scattera.

def plot_iris_correlations(iris):
    fig, ax = plt.subplots()

    colormap = cm.Accent
    colorlist = colormap(_map_factor_level(iris.classs))
    names = ("Iris-setosa", "Iris-versicolor", "Iris-virginica")
    for color, name in zip(colorlist, names):
        ax.scatter(iris.sepal_length, iris.petal_length, s=iris.sepal_width*10, c=color, label=name)

    ax.legend()

    plt.show()

Jedyne co to zamiast w każdym przebiegu for'a podawać mu wszystkie dane, musisz je rozgraniczyć. I będzie ładnie hulać :)
Nie znam struktury iris aby to zrobić, z poziomu plot'a tyle mogę pomóc :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1