简体   繁体   English

使用 Plotly Express 在相应的线上绘制数据点

[英]Using Plotly Express to plot data points on corresponding lines

Good day!再会! So I am working with this dictionary data set in python.所以我正在使用python中的这个字典数据集。 Say the dictionary looks like this:假设字典看起来像这样:

time_sheet = {"hours_dan":[10, 8, 8, 11], "hours_john":[9, 8, 11], "hours_alex":[10, 8]}

My goal is to generate a scatter plot and have their names on the y-axis, and their hours on the x-axis.我的目标是生成一个散点图,并在 y 轴上显示他们的名字,在 x 轴上显示他们的小时数。 It would give me three lines and their time points on each line corresponding to their hours.它会给我三行,每行上的时间点对应于他们的时间。

Is this achievable in Plotly Express?这在 Plotly Express 中可以实现吗? If not, what should I use to plot?如果没有,我应该用什么来绘制? Thank you so much!太感谢了!

To create a graph, the data format must be converted.要创建图表,必须转换数据格式。 Convert from dictionary format to data frame format.从字典格式转换为数据框格式。 Prepare an empty data frame and use a loop process to retrieve keys and values and merge them sequentially into a data frame.准备一个空数据框并使用循环过程来检索键和值并将它们按顺序合并到一个数据框中。 For graphs, symbol markers are added in addition to the basic form, and text positions are modified.对于图形,在基本形式的基础上增加了符号标记,并修改了文本位置。

import pandas as pd

df = pd.DataFrame()
time_sheet = {"hours_dan":[10, 8, 8, 11], "hours_john":[9, 8, 11], "hours_alex":[10, 8]}
for k,v in time_sheet.items():
    #print(k,v)
    df = pd.concat([df, pd.DataFrame({'name':k[6:], 'hours':v})],axis=0)

import plotly.express as px

fig = px.line(df, x='hours', y='name', color='name', text='hours', symbol='name')
fig.update_traces(textposition="bottom right")
fig.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM