使用Vincent创建多线图 [英] Creating a multiline graph using Vincent

查看:113
本文介绍了使用Vincent创建多线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Vincent创建多线图.

I am attempting to create a multiline graph using Vincent.

我有一个具有以下布局的csv文件:

I have a csv file with the following layout:

,wk1,wk2,wk3,wk4,wk5,wk6,wk7,wk8,wk9
Tom J,97,65,82,65,101,84,79,71,83
Lisa R,95,87,95,65,61,78,93,95,56
Rich F,51,111,50,119,84,77,73,84,60
Anne E,63,68,89,70,95,80,56,75,82
Dan M,83,95,36,115,79,79,65,55,69
Mack W,67,89,72,79,47,64,113,94,33

这是我的代码:

import pandas as pd
import vincent

df = pd.read_csv('weekscores.csv', index_col=0)

lines = vincent.Line(df)
lines.axis_titles(x='WEEKS', y='SCORE')
lines.legend(title='Player')
lines.to_json('line.html',html_out=True,html_path='line_template.html')

这会运行并生成图形,但图形中不会显示任何线段

This runs and a graph is generated but no lines are displayed in the graph:

使用.grammar()检查数据,我看到每周的得分是这样的:

Inspecting the data using .grammar() I see something like this for each week's score:

{'val': 97, 'col': 'wk1', 'idx': 'Tom J'}

感谢您提供任何帮助以使其呈现.

Any assistance in getting this to render is appreciated.

推荐答案

这里有两个问题:第一个问题是Vincent(天真的)假设折线图将采用线性比例尺,在这种情况下,我们实际需要序数刻度.第二个问题是,数据帧需要进行转置,以使星期在索引上.因此,要获取您要查找的图:

There are a couple issues here: the first is that Vincent (naively) assumes that line charts are going to take linear scales, when in this case we actual need an ordinal scale. The second issue is that the dataframe needs to be transposed so that the weeks are on the index. So, to get the plot you're looking for:

import vincent
import pandas as pd

df = pd.read_csv('weekscores.csv', index_col=0)
df = df.T
df
Out[27]: 
Name  Tom J  Lisa R  Rich F  Anne E  Dan M  Mack W
wk1      97      95      51      63     83      67
wk2      65      87     111      68     95      89
wk3      82      95      50      89     36      72
wk4      65      65     119      70    115      79
wk5     101      61      84      95     79      47
wk6      84      78      77      80     79      64
wk7      79      93      73      56     65     113
wk8      71      95      84      75     55      94
wk9      83      56      60      82     69      33

现在,我们已经翻转了数据,我们可以创建折线图,并确保x刻度为序数:

Now that we've got the data flipped, we can create the line chart, and ensure that the x-scale is ordinal:

lines = vincent.Line(df)
lines.scales[0].type = 'ordinal'
lines.axis_titles(x='WEEKS', y='SCORE')
lines.legend(title='Player')

,您应该得到如下所示的内容:

and you should end up with something like the following:

希望这会有所帮助!

这篇关于使用Vincent创建多线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆