绘制小提琴图的离散颜色序列

问题描述

如何将颜色序列 color_discrete_sequence=["green","blue","yellow","magenta"] 应用于下图中的“Sunday”、“Saturday”、“Thursday”、“Friday”,即周日应该是绿色,周六应该是蓝色等?

>
import plotly.graph_objects as go
import pandas as pd

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")

fig = go.figure()

fig.add_trace(go.Violin(x=df['day'],y=df['total_bill'],line_color='rgba(255,0.5)'
                       )
             )


fig.update_traces(Box_visible=False,meanline_visible=True,points='all',pointpos=-0,jitter=0.5,marker_line_color='rgba(0,0.5)',marker_line_width=1,showlegend=False)

fig.update_layout(template='simple_white')

fig.show()

给予

Violin plot

解决方法

Plotly express 似乎是正确的方法;看起来我装帧不对。

import plotly.express as px
import pandas as pd

df = px.data.tips()

fig = px.violin(df,x='day',y="total_bill",color='day',color_discrete_sequence=["green","blue","yellow","magenta"])

fig.update_traces(box_visible=False,meanline_visible=True,points='all',pointpos=-0,jitter=0.5,marker_line_color='rgba(0,0.5)',marker_line_width=1,showlegend=False)

fig.update_layout(template='simple_white')

fig.show()

给予

Violin plot with custom color sequence

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...