Jak dodać tabelę do działki seaborn (z przykładem)


Najłatwiejszym sposobem dodania tabeli do wykresu morskiego jest użycie funkcji table() programu Matplotlib.

Poniższy przykład pokazuje, jak w praktyce wykorzystać tę funkcję.

Przykład: Jak dodać tabelę do wykresu Seaborn

Załóżmy, że mamy następującą ramkę danych pand, która zawiera informacje o koszykarzach z różnych drużyn:

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' team ': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'],
                   ' points ': [18, 22, 19, 14, 14, 11, 20, 28, 30],
                   ' assists ': [5, 7, 7, 9, 12, 9, 9, 4, 15]})

#view DataFrame
print (df)

  team points assists
0 to 18 5
1 to 22 7
2 To 19 7
3 B 14 9
4 B 14 12
5 B 11 9
6 C 20 9
7 C 28 4
8 C 30 15

Poniższy kod pokazuje, jak utworzyć wykres rozrzutu w Seaborn i użyć funkcji table() Matplotlib, aby dodać tabelę pod wykresem, która wyświetla surowe wartości danych:

 import seaborn as sns
import matplotlib. pyplot as plt

#create scatterplot of assists vs points
sns. scatterplot (data=df, x=' assists ', y=' points ', hue=' team ')

#add table below scatterplot
table = plt. table (cellText= df.values ,
                  rowLabels=df. index ,
                  colLabels=df. columns ,
                  bbox=(.2, -.7, 0.5, 0.5))

#display final plot
plt. show () 

dodaj tabelę do działki Seaborn

Tabela pod wykresem przedstawia surowe wartości danych przedstawione na wykresie rozrzutu.

Argument bbox w funkcji table() steruje położeniem tabeli.

Argument bbox przyjmuje cztery wartości określające lewe, górne, prawe i dolne wypełnienie tabeli.

Możemy dostosować wartości argumentu bbox , aby umieścić tablicę po prawej stronie wykresu:

 import seaborn as sns
import matplotlib. pyplot as plt

#create scatterplot of assists vs points
sns. scatterplot (data=df, x=' assists ', y=' points ', hue=' team ')

#add table to the right of the scatterplot
table = plt. table (cellText= df.values ,
                  rowLabels=df. index ,
                  colLabels=df. columns ,
                  bbox=(1.1, .2, 0.5, 0.5))

#display final plot
plt. show () 

Seaborn dodaje tabelę po prawej stronie działki

Możesz swobodnie bawić się wartościami, aby umieścić stół w dokładnie takim miejscu, w jakim chcesz.

Uwaga : Pełną dokumentację funkcji table() Matplotlib można znaleźć tutaj .

Dodatkowe zasoby

Poniższe samouczki wyjaśniają, jak wykonywać inne typowe zadania za pomocą seaborn:

Jak dodać tytuł do działek Seaborn
Jak zmienić rozmiar czcionki w wykresach Seaborn
Jak dostosować liczbę kleszczy na działkach Seaborn

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *