I want to visualize csv files using Vega-Lite!

Trigger#

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.

Purpose#

I want to visualize my csv file using Vega-Lite

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

procedure#

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.

Step 1 Install Anaconda

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)

Step 2 Install IPython Vega

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.

Step 3 Actually use

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 + α ** visualization.png

Step 4 Use your csv file

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"}
  }
}
)

Procedure + α If it doesn't work

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 ...

Summary

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

I want to visualize csv files using Vega-Lite!
I want to email from Gmail using Python.
I tried to synthesize WAV files using Pydub.
[I want to classify images using Tensorflow] (2) Let's classify images
[Python] I want to merge Excel files anyway (pandas.merge)
I want to automate ssh using the expect command!
I want to solve Sudoku (Sudoku)
I want to automate ssh using the expect command! part2
I tried using "Syncthing" to synchronize files on multiple PCs
[TF] I tried to visualize the learning result using Tensorboard
[Python] Loading csv files using pandas
I want to understand systemd roughly
I want to scrape images to learn
I want to do ○○ with Pandas
I want to copy yolo annotations
I want to debug with Python
I tried to visualize BigQuery data using Jupyter Lab on GCP
For the time being, I want to convert files with ffmpeg !!
I want to pin Spyder to the taskbar
I want to detect objects with OpenCV
I want to output to the console coolly
I tried using Azure Speech to Text.
I want to print in a comprehension
I want to operate DB using Django's ORM from an external application
I tried to visualize AutoEncoder with TensorFlow
I want to handle the rhyme part1
I want to know how LINUX works!
I want to blog with Jupyter Notebook
I want to handle the rhyme part3
I want to use jar from python
I want to display an image on Jupyter Notebook using OpenCV (mac)
I want to build a Python environment
I want to use Linux on mac
I want to pip install with PythonAnywhere
Automatically update CSV files to AWS DynamoDB
I tried to classify text using TensorFlow
I want to convert a table converted to PDF in Python back to CSV
I want to analyze logs with Python
I want to play with aws with python
I want to use IPython Qt Console
I want to display the progress bar
I want to make an automation program!
I want to embed Matplotlib in PySimpleGUI
How to read CSV files in Pandas
I want to handle the rhyme part2
I want to develop Android apps on Android
I want CAPTCHA to say HIWAI words
I want to handle the rhyme part5
I want to handle the rhyme part4
I want to make a web application using React and Python flask
I tried to predict Covid-19 using Darts
I want to visualize where and how many people are in the factory
I want to visualize the transfer status of the 2020 J League, what should I do?
I want to see a list of WebDAV files in the Requests module
I want to get / execute variables / functions / classes of external files from Python
[Python] I want to collect specific files scattered in each folder in one place
I want to make matplotlib a dark theme
I want to connect to PostgreSQL from various languages
I want to do Dunnett's test in Python
I want to have recursion come to my mind
I want to easily create a Noise Model