【发布时间】:2021-12-23 04:20:44
【问题描述】:
我有一个数据集 df[type, x, y] - [96 rows x 3 columns]。 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 来制作 小提琴图,但是由于 我的 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 轴视为分类 - 所以不要拉伸它?
【问题讨论】:
-
将 x 转换为字符串?
-
我尝试使用 df["x"] = df["x"].astype("str") 并且我的类型是:type object x object y int64 dtype: object.但是图表是一样的。
标签: python pandas plotly violin-plot plotly-express