Skip to content
Advertisement

How to load and plot csv file using altair package in python?

I have a csv file which has 200000 lines and I would like to plot the data files using altair packagae. Documentation states that for large files, data needs to be passed as URL. This is what I have till now.

import  altair
alt.data_transformers.enable('csv')
url = 'path/to/data'

chart = alt.chart(url).mark_line.encode(x= 'time:T', y = 'current:Q')
chart.save('name.html')

But this does not seem to work. Am I missing something obvious here?

Advertisement

Answer

When you pass a dataset by URL and save the chart to HTML, the important thing is that the URL is valid for the web browser you use to view the HTML file.

So if you are viewing the chart locally and want to load a local file, use an appropriate file:// URL. If you plan to view the file within a web server that supports relative URLs for loading resources, pass the relative URL between the location of the HTML file and the location of the data file.

But, as a side-note, you mention your data has 200,000 rows: no matter how you pass the data to the Vega-Lite renderer, it’s unlikely to perform well with that much data. My personal rule-of-thumb is to avoid Altair/Vega-Lite for datasets with more than ~10,000 rows or so.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement