如何使用 plotly 显示这样的图形
How to display such a graph using plotly
I have a dataset like this
i want to display a graph like this using plotly (dash)
您可以使用 Plotly Express bar
图表。但是您首先必须将数据框从宽格式转换为长格式。
你的数据有一个完整的例子:
from io import StringIO
import pandas as pd
import plotly.express as px
data = """Director,Accountant,Foo,Texawaka,MeHenokep,Wed nosap,Programmer,Cy-wed
0,0,90,19,5,2,0,0
0,0,84,19,7,2,0,1
0,0,84,19,9,4,0,1
0,0,90,19,9,5,0,1
0,0,92,19,9,6,0,1
0,0,92,19,9,41,0,1
0,0,138,81,11,47,0,1
0,17,338,237,20,57,0,4
1,155,412,272,52,116,0,5
6,156,454,282,51,121,1,20
15,156,506,307,53,122,10,42
19,156,581,310,59,123,13,77
20,156,612,311,61,123,16,78
20,156,624,314,60,123,16,78
20,133,660,359,59,123,16,78
19,128,703,398,59,123,16,78
19,127,713,398,59,117,16,78
19,119,561,315,59,103,16,78
17,10,533,249,59,93,15,78
16,1,500,212,58,85,13,78
6,1,467,192,58,81,9,72
1,0,404,171,50,38,4,13
0,0,365,171,37,19,2,6
0,0,357,171,30,9,1,4
"""
# Create pandas.DataFrame
df = pd.read_csv(StringIO(data), sep=",").reset_index()
# Convert DataFrame from wide to long format
df_long = pd.melt(df, id_vars="index", var_name='position', value_name='value')
# Plot
fig = px.bar(df_long, x="index", y="value", color="position")
fig.show()
- 您的数据是图片,这意味着它不能用于答案。大多数代码正在生成具有代表性的样本数据集
- 这是一个直截了当的条形图。已设置 dtick 因此 x 轴中的每个值都有一个标签。已重新定位图例
import numpy as np
import random
import pandas as pd
import plotly.express as px
ROWS = 24
careers = [
"Director",
"Accountant",
"Programmer",
"Technician",
"Office Manager",
"Designer",
]
df = pd.DataFrame(
{
c: a[1][np.where(a[0] >= 0)[0][0] : np.where(a[0] == 0)[0][0] + ROWS]
for c, a in zip(
careers,
[
np.unique(
np.random.normal(
ROWS / 2, (ROWS / 2) / 2.33, random.randint(500, 1000)
).astype(int),
return_counts=True,
)
for _ in careers
],
)
}
)
px.bar(df, y=df.columns).update_layout(
xaxis={"dtick": 1, "title":""},
yaxis={"title":""},
legend={"yanchor": "bottom", "xanchor": "left", "x": 0, "y": -.2, "orientation": "h", "title":""},
)
I have a dataset like this
i want to display a graph like this using plotly (dash)
您可以使用 Plotly Express bar
图表。但是您首先必须将数据框从宽格式转换为长格式。
你的数据有一个完整的例子:
from io import StringIO
import pandas as pd
import plotly.express as px
data = """Director,Accountant,Foo,Texawaka,MeHenokep,Wed nosap,Programmer,Cy-wed
0,0,90,19,5,2,0,0
0,0,84,19,7,2,0,1
0,0,84,19,9,4,0,1
0,0,90,19,9,5,0,1
0,0,92,19,9,6,0,1
0,0,92,19,9,41,0,1
0,0,138,81,11,47,0,1
0,17,338,237,20,57,0,4
1,155,412,272,52,116,0,5
6,156,454,282,51,121,1,20
15,156,506,307,53,122,10,42
19,156,581,310,59,123,13,77
20,156,612,311,61,123,16,78
20,156,624,314,60,123,16,78
20,133,660,359,59,123,16,78
19,128,703,398,59,123,16,78
19,127,713,398,59,117,16,78
19,119,561,315,59,103,16,78
17,10,533,249,59,93,15,78
16,1,500,212,58,85,13,78
6,1,467,192,58,81,9,72
1,0,404,171,50,38,4,13
0,0,365,171,37,19,2,6
0,0,357,171,30,9,1,4
"""
# Create pandas.DataFrame
df = pd.read_csv(StringIO(data), sep=",").reset_index()
# Convert DataFrame from wide to long format
df_long = pd.melt(df, id_vars="index", var_name='position', value_name='value')
# Plot
fig = px.bar(df_long, x="index", y="value", color="position")
fig.show()
- 您的数据是图片,这意味着它不能用于答案。大多数代码正在生成具有代表性的样本数据集
- 这是一个直截了当的条形图。已设置 dtick 因此 x 轴中的每个值都有一个标签。已重新定位图例
import numpy as np
import random
import pandas as pd
import plotly.express as px
ROWS = 24
careers = [
"Director",
"Accountant",
"Programmer",
"Technician",
"Office Manager",
"Designer",
]
df = pd.DataFrame(
{
c: a[1][np.where(a[0] >= 0)[0][0] : np.where(a[0] == 0)[0][0] + ROWS]
for c, a in zip(
careers,
[
np.unique(
np.random.normal(
ROWS / 2, (ROWS / 2) / 2.33, random.randint(500, 1000)
).astype(int),
return_counts=True,
)
for _ in careers
],
)
}
)
px.bar(df, y=df.columns).update_layout(
xaxis={"dtick": 1, "title":""},
yaxis={"title":""},
legend={"yanchor": "bottom", "xanchor": "left", "x": 0, "y": -.2, "orientation": "h", "title":""},
)