简体   繁体   English

Plotly:如何从两行数据绘制折线图?

[英]Plotly: How to plot a line chart from two rows of data?

I'm trying to do a benchmarking line chart of a login times of a website between two groups of user.我正在尝试制作两组用户之间网站登录时间的基准折线图。 My dataframe is as follows:我的数据框如下:

df: df:

Group     Jan     Feb     Mar     Apr     May     June
A          12      62     44      34      15       25
B          55      43     42      29      42       33

How can I make a chart with two lines: A and B, with X axis being the months and y axis being the login times?如何制作包含两条线的图表:A 和 B,X 轴是月份,Y 轴是登录时间? I appreciate any assistance on this.我很感激这方面的任何帮助。 Thanks in advance!提前致谢!

The arguably easiest way to do this is transposing your dataframe using df.T , set the pandas plotly backend to plotly using pd.options.plotting.backend = "plotly" , and then just use df.plot() .可以说,最简单的方法是使用df.T转置数据帧,使用pd.options.plotting.backend = "plotly"将 Pandas 绘图后端设置为绘图,然后只使用df.plot() If you've got your exact data in a pandas dataframe, you only need to use:如果您在熊猫数据框中获得了确切数据,则只需使用:

df.set_index('Group').T.plot()

Plot:阴谋:

在此处输入图片说明

Complete code with data sample:带有数据示例的完整代码:

import pandas as pd
pd.options.plotting.backend = "plotly"

df = pd.DataFrame({'Group': {0: 'A', 1: 'B'},
                     'Jan': {0: 12, 1: 55},
                     'Feb': {0: 62, 1: 43},
                     'Mar': {0: 44, 1: 42},
                     'Apr': {0: 34, 1: 29},
                     'May': {0: 15, 1: 42},
                     'June': {0: 25, 1: 33}}).set_index('Group')

df.T.plot()

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

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