在 plotly express 小提琴图中将 x 轴视为分类
Treat x axis as categorical in plotly express violin plot
我有一个数据集 df[type, x, y] - [96 行 x 3 列]。 x 有 4 个唯一值:(0. 0.322, 1.89, 3.460)
这是一个示例:
print (df.groupby('x').apply(lambda df: df.sample(4)))
OUT:
type x y
x
0.000 5 2 0.000 1123
6 2 0.000 1178
7 2 0.000 850
3 1 0.000 515
0.322 72 1 0.322 174
42 1 0.322 182
79 2 0.322 450
10 1 0.322 340
1.890 54 2 1.890 140
71 2 1.890 126
80 1 1.890 61
19 1 1.890 60
3.460 30 2 3.460 120
88 1 3.460 35
26 1 3.460 40
92 2 3.460 98
我正在使用 plotly express 制作 violin plot,但是由于 我的 x 值不均匀间隔 ,它 拉伸我的 x 轴 很多,很难阅读:
fig = px.violin(df, y="y", x="x", color="type", box=True, violinmode='overlay',points="all",
hover_data=df.columns)
fig.show()
Plotly express 是否有办法将 x 轴视为分类轴 - 所以不拉伸它?
您可以将 xaxes 设置为分类:
fig.update_xaxes(type='category')
例如,我创建了一个与您的非常相似的 DataFrame,其中 x
列也是数字,现在 Plotly 将在绘图时将其解释为分类,每个唯一 x 值之间的间距均匀:
import numpy as np
import pandas as pd
import plotly.express as px
## create some data similar to yours with 80 rows
np.random.seed(42)
values = np.random.randint(30,size=80)
df = pd.DataFrame({
'x':[0,0.322,1.89,3.46]*20,
'y':np.random.randint(30,size=80),
'type':[1]*40 + [2]*40
})
fig = px.violin(df, y="y", x="x", color="type", box=True, violinmode='overlay',points="all",hover_data=df.columns)
fig.update_xaxes(type='category')
fig.show()
我找到了一个好的解决方法
我在df["x"]中添加了一个“-”字符
df["x"]=df['x'].astype(str) + "-"
fig = px.violin(df, y="y", x="x", color="type", box=True, violinmode='overlay',points="all",
hover_data=df.columns)
fig.show()
只是添加一个白色-space“”没用。
如果有人有更好的解决方案请post。
我有一个数据集 df[type, x, y] - [96 行 x 3 列]。 x 有 4 个唯一值:(0. 0.322, 1.89, 3.460)
这是一个示例:
print (df.groupby('x').apply(lambda df: df.sample(4)))
OUT:
type x y
x
0.000 5 2 0.000 1123
6 2 0.000 1178
7 2 0.000 850
3 1 0.000 515
0.322 72 1 0.322 174
42 1 0.322 182
79 2 0.322 450
10 1 0.322 340
1.890 54 2 1.890 140
71 2 1.890 126
80 1 1.890 61
19 1 1.890 60
3.460 30 2 3.460 120
88 1 3.460 35
26 1 3.460 40
92 2 3.460 98
我正在使用 plotly express 制作 violin plot,但是由于 我的 x 值不均匀间隔 ,它 拉伸我的 x 轴 很多,很难阅读:
fig = px.violin(df, y="y", x="x", color="type", box=True, violinmode='overlay',points="all",
hover_data=df.columns)
fig.show()
Plotly express 是否有办法将 x 轴视为分类轴 - 所以不拉伸它?
您可以将 xaxes 设置为分类:
fig.update_xaxes(type='category')
例如,我创建了一个与您的非常相似的 DataFrame,其中 x
列也是数字,现在 Plotly 将在绘图时将其解释为分类,每个唯一 x 值之间的间距均匀:
import numpy as np
import pandas as pd
import plotly.express as px
## create some data similar to yours with 80 rows
np.random.seed(42)
values = np.random.randint(30,size=80)
df = pd.DataFrame({
'x':[0,0.322,1.89,3.46]*20,
'y':np.random.randint(30,size=80),
'type':[1]*40 + [2]*40
})
fig = px.violin(df, y="y", x="x", color="type", box=True, violinmode='overlay',points="all",hover_data=df.columns)
fig.update_xaxes(type='category')
fig.show()
我找到了一个好的解决方法
我在df["x"]中添加了一个“-”字符
df["x"]=df['x'].astype(str) + "-"
fig = px.violin(df, y="y", x="x", color="type", box=True, violinmode='overlay',points="all",
hover_data=df.columns)
fig.show()
只是添加一个白色-space“”没用。
如果有人有更好的解决方案请post。