I am a newbie to data science and installed some data science module packages like Altair, Pandas, and so on. I was trying to display some data in Interactive Window of Python in VS Code. I put all my code at the bottom.
It was weird that I didn't see any data marks in the "chart" variable, but the next "chart" variable showed a data chart. I checked if the url was wrong, but actually it was fine. Here is some data of so many rows from the top.
manufacturer,model,displ,year,cyl,trans,drv,cty,hwy,fl,class
audi,a4,1.8,1999,4,auto(l5),f,18,29,p,compact
audi,a4,1.8,1999,4,manual(m5),f,21,29,p,compact
audi,a4,2,2008,4,manual(m6),f,20,31,p,compact
audi,a4,2,2008,4,auto(av),f,21,30,p,compact
audi,a4,2.8,1999,6,aut
Also, let me add a picture of the second chart with marks I could see.
# %%
msg = "Hello World"
print(msg)
# %%
msg = "Hello again"
print(msg)
# %%
import sys
!{sys.executable} -m pip install altair_saver
# %%
import pandas as pd
import altair as alt
# %%
alt.data_transformers.enable('json')
# %%
# %%
import altair as alt
import pandas as pd
url = "https://github.com/byuidatascience/data4python4ds/raw/master/data-raw/mpg/mpg.csv"
mpg = pd.read_csv(url)
chart = alt.Chart(mpg).mark_point().encode(
x='displ',
y='hwy'
)
chart
# chart.save("screenshots/altair_viz_1.png")
# %%
import altair as alt
alt.__version__
# %%
import altair as alt
from vega_datasets import data
cars = data.cars.url
chart = alt.Chart(cars).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color='Origin:N',
)
chart
# %%
Thanks for your valuable time in advance.
If you remove alt.data_transformers.enable('json')
, your first example will work (the second is not affected by this line since it uses a URL rather than a dataframe).
import altair as alt
import pandas as pd
url = "https://github.com/byuidatascience/data4python4ds/raw/master/data-raw/mpg/mpg.csv"
mpg = pd.read_csv(url)
chart = alt.Chart(mpg).mark_point().encode(
x='displ',
y='hwy'
)
chart
There is related discussion here https://github.com/altair-viz/altair/issues/1732. I don't know if there is a reliable way of making the json datatransformer work with multiple front ends such as jupyterlab, vscode etc, but you can often use another transformer instead, e.g. https://github.com/altair-viz/altair_data_server