私はで動作するようにしようとしていますplotly
具体的には、ploty express
いくつかの視覚化を構築するために、。
私が構築していますことの一つは、散布図
私は素敵な散布図を作成し、以下のいくつかのコードを、持っています:
import plotly.graph_objs as go, pandas as pd, plotly.express as px
df = pd.read_csv('iris.csv')
fig = px.scatter(df, x='sepal_length', y='sepal_width',
color='species', marker_colorscale=px.colors.sequential.Viridis)
fig.show()
しかし、私はカラースキームを試してみて、変更したい、つまり、色はそれぞれの種のために提示しました。
読みました:
- https://plotly.com/python/builtin-colorscales/
- https://plotly.com/python/colorscales/
- https://plotly.com/python/v3/colorscales/
しかし、変化に色を取得することはできません。
しよう:
fig = px.scatter(df, x='sepal_length', y='sepal_width',
color='species', marker_colorscale=px.colors.sequential.Viridis)
利回り:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-78a9d58dce23> in <module>
2 # https://plotly.com/python/line-and-scatter/
3 fig = px.scatter(df, x='sepal_length', y='sepal_width',
----> 4 color='species', marker_colorscale=px.colors.sequential.Viridis)
5 fig.show()
TypeError: scatter() got an unexpected keyword argument 'marker_colorscale'
しよう
しよう:
fig = px.scatter(df, x='sepal_length', y='sepal_width',
color='species', continuous_colorscale=px.colors.sequential.Viridis)
利回り:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-78a9d58dce23> in <module>
2 # https://plotly.com/python/line-and-scatter/
3 fig = px.scatter(df, x='sepal_length', y='sepal_width',
----> 4 color='species', continuous_colorscale=px.colors.sequential.Viridis)
5 fig.show()
TypeError: scatter() got an unexpected keyword argument 'continuous_colorscale'
どのように私はで使用される色に変更することができますplotly
可視化を?
一般に、plotlyエクスプレス図形の配色を変更することは、非常に単純です。何ここでの問題を引き起こしていることは事実であるspecies
であるカテゴリ変数。連続または数値は実際に簡単ですが、私たちは少しのものに取得します。
カテゴリ値の場合は、使用してはcolor_discrete_map
面倒なアプローチとはいえ、完全に有効です。私はキーワード引数を使用して好むcontinuous_colorscale
との組み合わせでpx.colors.qualitative.Antique
、Antique
任意に変更することができる別個のカラースキーム plotly発現に利用可能。ただ、実行dir(px.colors.qualitative)
、実行しているplotlyバージョンではあなたに利用できるかを確認します。
['Alphabet',
'Antique',
'Bold',
'D3',
'Dark2',
'Dark24',
'G10',......]
コード1:
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length",
color="species", color_discrete_sequence=px.colors.qualitative.Antique)
fig.show()
プロット1:
それでは、連続変数についてはどうですか?
次のスニペットを考えてみます。
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length",
color="sepal_length", color_continuous_scale=px.colors.sequential.Viridis)
fig.show()
これを実行すると、このプロットを生成します:
You can change the colors to any other theme available under dir(px.colors.sequential)
, for example color_continuous_scale=px.colors.sequential.Inferno
, and get this plot:
What's possibly causing confusion here, is that setting color='species
, and keeping color_continuous_scale=px.colors.sequential.Inferno
will give you this plot:
The figure now jumps straight back to using the default plotly colors, without giving you any warning about color_continuous_scale=px.colors.sequential.Inferno
not having an effect. This is becauce species
is a categorical variable with these different values : ['setosa', 'versicolor', 'virginica']
, so color_continuous_scale
is simply ignored. For color_continuous_scale
to take effect you'll have to use a numerical value, like `sepal_length = [5.1, 4.9, 4.7, 4.6, 5. , 5.4, ...]
And this brings us right back to my initial answer for categorical values:
Use the keyword argument
continuous_colorscale
in combination withpx.colors.qualitative