Plotsdecoratie
Stijl instellen
seaborn
biedt de functie set_style()
specifiek voor het instellen van de visuele stijl van uw grafieken. Deze functie vereist één verplichte parameter genaamd style
. De parameter style
accepteert verschillende vooraf gedefinieerde opties, die elk een onderscheidende stijl vertegenwoordigen:
'white'
'dark'
'whitegrid'
'darkgrid'
'ticks'
Experimenteer gerust met deze opties:
import seaborn as sns import matplotlib.pyplot as plt # Setting the style sns.set_style('darkgrid') titanic_df = sns.load_dataset('titanic') sns.countplot(data=titanic_df, x='class') plt.show()
Palet instellen
Een andere mogelijkheid is het wijzigen van de kleuren van plot-elementen in seaborn
met behulp van de functie set_palette()
, waarbij de enige verplichte parameter palette
centraal staat:
Circulaire paletten:
'hls'
,'husl'
;Perceptueel uniforme paletten:
'rocket'
,'magma'
,'mako'
, enz.;Divergerende kleurpaletten:
'RdBu'
,'PRGn'
, enz.;Sequentiële kleurpaletten:
'Greys'
,'Blues'
, enz.
Je kunt meer ontdekken over verschillende paletten in het "Choosing color palettes" artikel.
import seaborn as sns import matplotlib.pyplot as plt # Setting the style sns.set_style('darkgrid') # Setting the palette sns.set_palette('magma') # Loading a built-in dataset of the Titanic passengers titanic_df = sns.load_dataset('titanic') sns.countplot(data=titanic_df, x='class') plt.show()
Context Instellen
Er is een andere functie in de seaborn
-bibliotheek, set_context()
. Deze beïnvloedt aspecten zoals de grootte van de labels, lijnen en andere elementen van de plot (de algemene stijl wordt niet beïnvloed).
De belangrijkste parameter is context
, die een dict
van parameters kan zijn of een string
die de naam van een vooraf ingestelde set vertegenwoordigt.
De standaard context
is 'notebook'
. Andere beschikbare contexten zijn 'paper'
, 'talk'
en 'poster'
, die in wezen geschaalde versies zijn van de notebook
-parameters.
import seaborn as sns import matplotlib.pyplot as plt # Setting the style sns.set_style('darkgrid') # Setting the palette sns.set_palette('magma') # Setting the context sns.set_context('paper') # Loading a built-in dataset of the Titanic passengers titanic_df = sns.load_dataset('titanic') sns.countplot(data=titanic_df, x='class') plt.show()
Je kunt meer ontdekken in de set_context()
documentatie.
Swipe to start coding
- Gebruik de juiste functie om de stijl in te stellen op
'dark'
. - Gebruik de juiste functie om het palet in te stellen op
'rocket'
. - Gebruik de juiste functie om de context in te stellen op
'talk'
.
Oplossing
Bedankt voor je feedback!