将alt.Chart()与来自Google驱动器URL链接的数据一起使用

时间:2019-05-19 22:28:11

标签: python json google-colaboratory altair

我正在与altair一起在Google colab中密谋。我遇到了“最大行”警告:https://altair-viz.github.io/user_guide/faq.html#maxrowserror-how-can-i-plot-large-datasets

这是我的数据集的标题:df.head()

所以现在我想通过URL传递数据,就像我的googleDrive: 首先,我将文件导出到驱动器:

"change directory and export whole csv "
os.chdir(Directory.table_dir)
one.to_json('one.json', orient='records')

然后我尝试使用URLData方法:https://altair-viz.github.io/user_guide/generated/core/altair.UrlData.html#altair.UrlData

os.chdir(Directory.table_dir)
#checking if i can read the file to a pandas dataframe
df=pd.read_json('one.json', orient='records')

source=alt.UrlData('content/gdrive/My Drive/SCTFT/Tables/one.json')


chart = alt.Chart(source).mark_point().encode(
    x='VG:Q',
    y='absID:Q',
    color='file:N',
)
chart

我也尝试过:

source='content/gdrive/My Drive/SCTFT/Tables/one.json'

使用df从matplotlib进行绘图。

但是从altair我得到: altair plot

我没有收到任何错误消息。 我应该更改导出文件的方式吗?或我如何将其与网址链接?

使用新信息进行编辑

我从https://colab.research.google.com/github/altair-viz/altair_data_server/blob/master/AltairDataServer.ipynb运行了代码  :pip install first graph

一直有效,直到:

Altair data server

这只是笔记本电脑的正常运行,没有进行任何更改,因此我的colab的设置必须存在问题吗?

1 个答案:

答案 0 :(得分:0)

您传递给图表的URL数据必须通过HTTP请求对前端可见,并且content/gdrive/My Drive/SCTFT/Tables/one.json看起来不是有效的URL。

由于您的数据位于Google驱动器上,并且无法通过HTTP URL获得,因此我建议使用disabling the maximum rows check并将数据框直接传递到图表:

alt.data_transformers.enable(max_rows=None)

alt.Chart(df).mark_point().encode(
    x='VG:Q',
    y='absID:Q',
    color='file:N',
)