Legendan Lisääminen
Kun kaaviossa on useita elementtejä, niiden nimeäminen selkeyden vuoksi on usein hyödyllistä. Selite (legend) palvelee tätä tarkoitusta tarjoamalla tiiviin alueen, jossa selitetään kaavion eri osat.
Seuraavassa on kolme yleistä tapaa luoda selite matplotlib
-kirjastossa.
Ensimmäinen vaihtoehto
Tarkastellaan seuraavaa esimerkkiä käsitteen havainnollistamiseksi:
import matplotlib.pyplot as plt import numpy as np # Define categories and data questions = ['question_1', 'question_2', 'question_3'] yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) # Set positions and bar width positions = np.arange(len(questions)) width = 0.3 # Create the grouped bar chart for i in range(len(answers)): plt.bar(positions + width * i, answers[i], width) # Adjust x-axis ticks to the center of groups plt.xticks(positions + width * (len(answers) - 1) / 2, questions) # Setting the labels for the legend explicitly plt.legend(['positive answers', 'negative answers']) plt.show()
Vasemman yläkulman selite kuvaa kaavion eri pylväät. Tämä selite luodaan käyttämällä funktiota plt.legend()
, jolle annetaan ensimmäisenä argumenttina lista nimikkeistä—yleisesti nimellä labels
.
Toinen vaihtoehto
Toinen vaihtoehto on määrittää label
-parametri jokaisessa piirtofunktion, kuten bar, kutsussa:
import matplotlib.pyplot as plt import numpy as np # Define x-axis categories and their positions questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) # Define answers for each category yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) labels = ['positive answers', 'negative answers'] # Set the width for each bar width = 0.3 # Plot each category with a label for i in range(len(answers)): plt.bar(positions + width * i, answers[i], width, label=labels[i]) # Set x-axis ticks and labels at the center of each group plt.xticks(positions + width * (len(answers) - 1) / 2, questions) # Automatically create legend from label parameters plt.legend() plt.show()
Tässä plt.legend()
määrittää automaattisesti legendaan lisättävät elementit ja niiden selitteet; kaikki elementit, joille on määritetty label-parametri, sisällytetään.
Kolmas vaihtoehto
Itse asiassa on olemassa vielä yksi vaihtoehto käyttämällä set_label()
-metodia artistille (esimerkissämme bar
):
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) width = 0.3 labels = ['positive answers', 'negative answers'] # Plot bars for each category with labels for i in range(len(answers)): bar = plt.bar(positions + width * i, answers[i], width) bar.set_label(labels[i]) # Set x-axis ticks and labels at the center of the grouped bars center_positions = positions + width * (len(answers) - 1) / 2 plt.xticks(center_positions, questions) # Display legend above the plot, centered horizontally plt.legend(loc='upper center') plt.show()
Selitteen sijainti
Toinen tärkeä avainsana legend()
-funktiossa on loc
, joka määrittää selitteen sijainnin. Oletusarvo on best
, mikä "ohjaa" matplotlib
-kirjastoa valitsemaan automaattisesti parhaan sijainnin selitteelle, jotta se ei peitä datan osia.
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) width = 0.3 labels = ['positive answers', 'negative answers'] # Plot bars for each category with labels for i, label in enumerate(labels): bars = plt.bar(positions + width * i, answers[i], width) bars.set_label(label) # Set x-axis ticks and labels at the center of the grouped bars center_positions = positions + width * (len(answers) - 1) / 2 plt.xticks(center_positions, questions) # Display legend above the plot, centered horizontally plt.legend(loc='upper center') plt.show()
Tässä esimerkissä selite sijoitetaan kuvaajan yläosaan keskelle. Muita kelvollisia arvoja loc
-parametrille ovat:
'upper right'
,'upper left'
,'lower left'
;'lower right'
,'right'
;'center left'
,'center right'
,'lower center'
,'center'
.
Voit tutustua aiheeseen tarkemmin legend()
dokumentaatiossa
Swipe to start coding
- Merkitse alimmat palkit nimellä
'primary sector'
käyttämällä sopivaa avainsana-argumenttia. - Merkitse keskimmäiset palkit nimellä
'secondary sector'
käyttämällä sopivaa avainsana-argumenttia. - Merkitse ylimmät palkit nimellä
'tertiary sector'
käyttämällä sopivaa avainsana-argumenttia. - Sijoita selite oikealle puolelle, pystysuunnassa keskelle.
Ratkaisu
Kiitos palautteestasi!