セクション 5. 章 3
single
プロットの装飾
メニューを表示するにはスワイプしてください
スタイルの設定
seaborn では、プロットのビジュアルスタイルを設定するために set_style() 関数を提供しています。この関数には、必須パラメータである style が必要です。style パラメータには、いくつかの事前定義されたオプションがあり、それぞれ異なるスタイルを表します。
'white''dark''whitegrid''darkgrid''ticks'
これらを自由に試すことができます:
12345678910import 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()
パレットの設定
もう一つのオプションとして、seaborn の set_palette() 関数を使ってプロット要素の色を変更できます。ここでは、唯一の必須パラメータである palette に注目します。
- 円形パレット:
'hls','husl'; - 知覚的に均一なパレット:
'rocket','magma','mako'など; - 発散型カラーパレット:
'RdBu','PRGn'など; - 連続型カラーパレット:
'Greys','Blues'など。
さらに学ぶ
さまざまなパレットについてさらに詳しく知りたい場合は、 「Choosing color palettes」の記事をご覧ください。
1234567891011121314import 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()
コンテキストの設定
seabornライブラリには、set_context()という別の関数があります。これは、ラベル、線、およびプロットの他の要素のサイズなどに影響します(全体のスタイルには影響しません)。
最も重要なパラメータはcontextで、これはパラメータのdictまたは事前設定されたセット名を表すstringのいずれかです。
デフォルトのcontextは'notebook'です。他に利用可能なコンテキストとしては、'paper'、'talk'、'poster'があり、これらは本質的にnotebookパラメータのスケールバージョンです。
1234567891011121314151617import 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()
さらに学ぶ
さらに詳しくは、 set_context() ドキュメントを参照。
タスク
スワイプしてコーディングを開始
- 正しい関数を使用してスタイルを
'dark'に設定してください。 - 正しい関数を使用してパレットを
'rocket'に設定してください。 - 正しい関数を使用してコンテキストを
'talk'に設定してください。
解答
すべて明確でしたか?
フィードバックありがとうございます!
セクション 5. 章 3
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください