string or categorical integer values considered as numeric in strip/box/violin plots
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 18.8k
- フォーク
- 2.8k
- 平均マージ
- 16時間 26分
- マージ済み PR(30日)
- 21
説明
Issue:
When using integer values as categorical variable in a strip / box / violin plot, the values of the categorical variable are mapped to a continuous numeric axis even if the values are of string or pd.Categorical type.
Example:
We create a dataframe with columns names having an integer value in string format. These could be any category that makes sense to the specific business case (e.g. product code, etc.)
import numpy as np
import pandas as pd
import plotly_express as px
n = 50
df = pd.DataFrame({
'1': np.random.normal(2, .3, n),
'2': np.random.lognormal(.5, .2, n),
'34': np.random.triangular(0, 2, 3, n),
'123': np.random.uniform(1, 3, n)
})
We unpivot the data using and make a strip plot.
df1 = df.melt()
px.strip(df1, x='variable', y='value')

The categorical variable (values '1', '2', '34' and '123') get mapped to a continuous numeric scale. Here, the variables '1' and '2' blend together and this can get worst if there are orders of magnitude between the different values.
Converting the string values to pd.Categorical type yields the same result as above.
df2 = df.melt()
df2.variable = pd.Categorical(df2.variable)
px.strip(df2, x='variable', y='value')
Workaround:
Adding a character to the values makes them be recognized as categorical which is the expected result (except for the added character in the category names). Unfortunately, adding a blank space does not work either.
df3 = df.copy()
df3.columns = [f"c{c}" for c in df3.columns]
px.strip(df3.melt(), x='variable', y='value')

Considering that numeric categorical values are legitimate in many contexts, it should be possible to use numbers as categories if they are represented by a string or categorical data type, as is the case with the color parameter (https://github.com/plotly/plotly_express/issues/140):
px.strip(df.melt(), y='value', color='variable')

Thanks!
Package Version
-------------------- ---------
plotly 4.1.1
plotly-express 0.4.1
@emmanuelle, this is one of the two issues we discussed at the PyData meetup.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Start with the px.strip(df.melt(), x='variable', y='value') reproducer and compare it with the color='variable' example. Trace how categorical values are inferred for strip, box, and violin plots; done means string and pd.Categorical labels containing integer-like values remain discrete categories without a prefix. No file or test location is named.
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- numpy, pandas, python
- 領域
- data-visualization
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100