I decided to use Vega-Lite in the laboratory, but I didn't know what to do after the tutorial, and it was quite difficult, so I hope it helps me with a memorandum + someone.
Tutorial is officially provided Online Editor I can proceed without problems by using / edited), but I don't know if there is no way to upload the file I have or if I just couldn't find it, but I couldn't, so Vega- I searched for a way to use Lite
As a result of various investigations
That's why I tried to use Vega-Lite with Jupyter Lab, but from the conclusion, I used Jupyter Notebook because I got an error in Jupyter Lab for some reason in my environment.
It is not essential to use JupyterLab, but if you want to visualize data, we recommend installing Anaconda because it has libraries that you often use. If you are used to it, you can follow the official (https://www.anaconda.com/) page, but this is easy to understand. ..
-[Download Anaconda] (https://qiita.com/kanta13jp1/items/c6cc86dfd31a1c536aa9)
I don't understand it properly, but it seems to be for using Vega-Lite with jupyter notebook. Originally it seems that JupyterLab is not needed, but before installing this, when I tried to import vega with JupyterLab, I was told that there was a version problem or there was no module, so I installed it. It's okay if you follow this page (https://github.com/vega/ipyvega), but since it's in English, I'll post the code for the time being. ..
In Anaconda, I saw it somewhere without using pip, so it may be okay to skip it, but at the command prompt
pip install jupyter pandas vega
pip install --upgrade notebooksys-prefix below
jupyter nbextension install --sys-prefix --py vega
To execute.
Also, then do the following
conda install vega
You can now use Vega-Lite.
Start JupyterLab, select python3 of Notebook and write the code in the editor.
from vega import VegaLite
VegaLite(
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"data": {
"values": [
{"a": "C", "b": 2},
{"a": "C", "b": 7},
{"a": "C", "b": 4},
{"a": "D", "b": 1},
{"a": "D", "b": 2},
{"a": "D", "b": 6},
{"a": "E", "b": 8},
{"a": "E", "b": 4},
{"a": "E", "b": 7}
]
},
"mark": "point",
"encoding": {
"x": {"field": "a", "type": "nominal"},
"y": {"field": "b", "type": "quantitative"}
}
}
)
You can use Vega-Lite on the first line, and after that, if you write the code like the one you did in the tutorial in VegaLite (), it will work. By the way, the code written here is for the tutorial, and when executed, the following figure will be displayed. ** If it doesn't work, see step + α **
Finally, I will use the files I have. When you finish the first tutorial, there is next tutorial at the bottom, and when you go there, you bring the file from the outside You can see that there is the following code.
{
"data": {"url": "data/seattle-weather.csv"},
"mark": "tick",
"encoding": {
"x": {"field": "precipitation", "type": "quantitative"}
}
}
But here, I think there are people who think URL ?. ~~ I thought. ~~ I feel that only files that are online can be used. However, that was not the case, and it worked if I changed the part of data / seattle-weather.csv
to the path of the file I wanted to use.
In other words, the actual pyhton code you write looks like this.
from vega import VegaLite
VegaLite(
{
"data": {"url": "Downloads/hoge.csv"}, #The path of the file you want to use
#Specification of data used below
"mark": "tick",
"encoding": {
"x": {"field": "precipitation", "type": "quantitative"}
}
}
)
I don't know the cause, but I couldn't get the following error when I tried this procedure.
Javascript Error: require is not defined
I knew that it was a Javascript error, but when I looked it up, I could only get information that seemed impossible to use the language, and I couldn't figure out how to solve it. If you get the same error, try using Jupyter Notebook instead of Jupyter Lab. It's very painful not to know the reason, but very much I worked with this. Will it be cured if I reinstall Anaconda or Jupyter Lab ...
Since this is my first time posting an article, I would appreciate it if you could point out the bad part of the method and the content of the article itself. There are a lot of things I don't understand myself, but there are almost only English materials for Vega-Lite, and is it in one language but in another? tool? It's my first time to use, and I've been researching it for days, so I hope it helps someone even a little.
Recommended Posts