altairを使ってvega datasetsのデータをチャート表示する

その買うを、もっとハッピーに。|ハピタス

今回はこのサイトを参考にして、vega datasetsのデータをaltairを使ってjupyter notebookにチャート化して表示させてみる。

スポンサーリンク

vega datasetsとaltair

必要なモジュールをインポートしてチャートを表示させる。

# load an example dataset
from vega_datasets import data
cars = data.cars()

# plot the dataset, referencing dataframe column names
import altair as alt
alt.Chart(cars).mark_bar().encode(
  x='mean(Miles_per_Gallon)',
  y='Origin',
  color='Origin'
)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-129c960b7f01> in <module>()
      1 # load an example dataset
----> 2 from vega_datasets import data
      3 cars = data.cars()
      4 
      5 # plot the dataset, referencing dataframe column names

ModuleNotFoundError: No module named 'vega_datasets'

vega_datasetsモジュールをインストールする。

!pip3 install vega_datasets
Collecting vega_datasets
  Downloading https://files.pythonhosted.org/packages/47/c1/62c6cbdf7723209634826e0a96806d8493955fcf35d71c2ff870054b0a9a/vega_datasets-0.5.0-py2.py3-none-any.whl (201kB)
    100% |################################| 204kB 1.8MB/s ta 0:00:01
Requirement already satisfied: pandas in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from vega_datasets) (0.23.1)
Requirement already satisfied: numpy>=1.9.0 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from pandas->vega_datasets) (1.14.4)
Requirement already satisfied: pytz>=2011k in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from pandas->vega_datasets) (2018.4)
Requirement already satisfied: python-dateutil>=2.5.0 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from pandas->vega_datasets) (2.7.3)
Requirement already satisfied: six>=1.5 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from python-dateutil>=2.5.0->pandas->vega_datasets) (1.11.0)
Installing collected packages: vega-datasets
Successfully installed vega-datasets-0.5.0
# load an example dataset
from vega_datasets import data
cars = data.cars()

# plot the dataset, referencing dataframe column names
import altair as alt
alt.Chart(cars).mark_bar().encode(
  x='mean(Miles_per_Gallon)',
  y='Origin',
  color='Origin'
)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-3-129c960b7f01> in <module>()
      4 
      5 # plot the dataset, referencing dataframe column names
----> 6 import altair as alt
      7 alt.Chart(cars).mark_bar().encode(
      8   x='mean(Miles_per_Gallon)',

ModuleNotFoundError: No module named 'altair'

altairをインストールする。

!pip3 install git+https://github.com/altair-viz/altair
Collecting git+https://github.com/altair-viz/altair
  Cloning https://github.com/altair-viz/altair to /tmp/pip-req-build-g1jwghba
Requirement already satisfied: entrypoints in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from altair==2.3.0.dev0) (0.2.3)
Requirement already satisfied: jinja2 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from altair==2.3.0.dev0) (2.10)
Requirement already satisfied: jsonschema in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from altair==2.3.0.dev0) (2.6.0)
Requirement already satisfied: numpy in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from altair==2.3.0.dev0) (1.14.4)
Requirement already satisfied: pandas in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from altair==2.3.0.dev0) (0.23.1)
Requirement already satisfied: six in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from altair==2.3.0.dev0) (1.11.0)
Requirement already satisfied: toolz in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from altair==2.3.0.dev0) (0.9.0)
Requirement already satisfied: typing in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages/typing-3.6.4-py3.6.egg (from altair==2.3.0.dev0) (3.6.4)
Requirement already satisfied: MarkupSafe>=0.23 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from jinja2->altair==2.3.0.dev0) (1.0)
Requirement already satisfied: pytz>=2011k in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from pandas->altair==2.3.0.dev0) (2018.4)
Requirement already satisfied: python-dateutil>=2.5.0 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from pandas->altair==2.3.0.dev0) (2.7.3)
Building wheels for collected packages: altair
  Running setup.py bdist_wheel for altair ... done
  Stored in directory: /tmp/pip-ephem-wheel-cache-xo2ze5fm/wheels/bc/c8/58/34fe9904ff5a6d2e99a590d4b1f67961ff1145fc21df7766ff
Successfully built altair
Installing collected packages: altair
Successfully installed altair-2.3.0.dev0
# load an example dataset
from vega_datasets import data
cars = data.cars()

# plot the dataset, referencing dataframe column names
import altair as alt
alt.Chart(cars).mark_bar().encode(
  x='mean(Miles_per_Gallon)',
  y='Origin',
  color='Origin'
)
<VegaLite 2 object>

If you see this message, it means the renderer has not been properly enabled
for the frontend that you are using. For more information, see
https://altair-viz.github.io/user_guide/troubleshooting.html

alt.renderers.enable(‘notebook’)を付け加える。

# load an example dataset
from vega_datasets import data
cars = data.cars()

# plot the dataset, referencing dataframe column names
import altair as alt
alt.renderers.enable('notebook') ##ここに付け加える
alt.Chart(cars).mark_bar().encode(
  x='mean(Miles_per_Gallon)',
  y='Origin',
  color='Origin'
)
---------------------------------------------------------------------------
NoSuchEntryPoint                          Traceback (most recent call last)
~/.pyenv/versions/py365/lib/python3.6/site-packages/altair/utils/plugin_registry.py in _enable(self, name, **options)
    133             try:
--> 134                 ep = entrypoints.get_single(self.entry_point_group, name)
    135             except entrypoints.NoSuchEntryPoint:

~/.pyenv/versions/py365/lib/python3.6/site-packages/entrypoints.py in get_single(group, name, path)
    185 
--> 186     raise NoSuchEntryPoint(group, name)
    187 

NoSuchEntryPoint: No 'notebook' entry point found in group 'altair.vegalite.v2.renderer'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-7-a7606151b2c3> in <module>()
      5 # plot the dataset, referencing dataframe column names
      6 import altair as alt
----> 7 alt.renderers.enable('notebook')
      8 alt.Chart(cars).mark_bar().encode(
      9   x='mean(Miles_per_Gallon)',

~/.pyenv/versions/py365/lib/python3.6/site-packages/altair/utils/plugin_registry.py in enable(self, name, **options)
    169         if name is None:
    170             name = self.active
--> 171         return PluginEnabler(self, name, **options)
    172 
    173 

~/.pyenv/versions/py365/lib/python3.6/site-packages/altair/utils/plugin_registry.py in __init__(self, registry, name, **options)
     23         self.options = options
     24         self.original_state = registry._get_state()
---> 25         self.registry._enable(name, **options)
     26 
     27     def __enter__(self):

~/.pyenv/versions/py365/lib/python3.6/site-packages/altair/utils/plugin_registry.py in _enable(self, name, **options)
    135             except entrypoints.NoSuchEntryPoint:
    136                 if name in self.entrypoint_err_messages:
--> 137                     raise ValueError(self.entrypoint_err_messages[name])
    138                 else:
    139                     raise

ValueError: 
To use the 'notebook' renderer, you must install the vega package
and the associated Jupyter extension.
See https://altair-viz.github.io/getting_started/installation.html
for more information.

上のサイトの指示通りに必要なモジュールをインストールする。

!pip3 install -U altair vega_datasets notebook vega
Requirement already up-to-date: altair in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (2.3.0.dev0)
Requirement already up-to-date: vega_datasets in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (0.5.0)
Collecting notebook
  Downloading https://files.pythonhosted.org/packages/5e/7c/7fd8e9584779d65dfcad9fa2e09c76131a41f999f853a9c7026ed8585586/notebook-5.6.0-py2.py3-none-any.whl (8.9MB)
    100% |################################| 8.9MB 8.5MB/s  eta 0:00:01
Collecting vega
  Downloading https://files.pythonhosted.org/packages/0a/ce/1ab1bf943f9b80f9e87d807a29337daff93cb64c7080b37633f4655b8c21/vega-1.4.0.tar.gz (1.1MB)
    100% |################################| 1.1MB 33.7MB/s ta 0:00:01
Requirement already satisfied, skipping upgrade: typing in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages/typing-3.6.4-py3.6.egg (from altair) (3.6.4)
Requirement already satisfied, skipping upgrade: toolz in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from altair) (0.9.0)
Requirement already satisfied, skipping upgrade: pandas in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from altair) (0.23.1)
Requirement already satisfied, skipping upgrade: jinja2 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from altair) (2.10)
Requirement already satisfied, skipping upgrade: entrypoints in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from altair) (0.2.3)
Requirement already satisfied, skipping upgrade: six in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from altair) (1.11.0)
Requirement already satisfied, skipping upgrade: numpy in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from altair) (1.14.4)
Requirement already satisfied, skipping upgrade: jsonschema in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from altair) (2.6.0)
Requirement already satisfied, skipping upgrade: Send2Trash in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from notebook) (1.5.0)
Requirement already satisfied, skipping upgrade: nbconvert in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from notebook) (5.3.1)
Requirement already satisfied, skipping upgrade: ipython-genutils in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from notebook) (0.2.0)
Requirement already satisfied, skipping upgrade: jupyter-core>=4.4.0 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from notebook) (4.4.0)
Collecting prometheus-client (from notebook)
  Downloading https://files.pythonhosted.org/packages/a1/b1/08de091b392fec31da9bd3f5edd9214ec1c6931dd81641610ac20f3ff934/prometheus_client-0.3.1.tar.gz
Requirement already satisfied, skipping upgrade: traitlets>=4.2.1 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from notebook) (4.3.2)
Requirement already satisfied, skipping upgrade: pyzmq>=17 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from notebook) (17.0.0)
Requirement already satisfied, skipping upgrade: tornado>=4 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from notebook) (5.0.2)
Requirement already satisfied, skipping upgrade: ipykernel in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from notebook) (4.8.2)
Requirement already satisfied, skipping upgrade: jupyter-client>=5.2.0 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from notebook) (5.2.3)
Requirement already satisfied, skipping upgrade: nbformat in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from notebook) (4.4.0)
Requirement already satisfied, skipping upgrade: terminado>=0.8.1 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from notebook) (0.8.1)
Requirement already satisfied, skipping upgrade: python-dateutil>=2.5.0 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from pandas->altair) (2.7.3)
Requirement already satisfied, skipping upgrade: pytz>=2011k in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from pandas->altair) (2018.4)
Requirement already satisfied, skipping upgrade: MarkupSafe>=0.23 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from jinja2->altair) (1.0)
Requirement already satisfied, skipping upgrade: bleach in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from nbconvert->notebook) (1.5.0)
Requirement already satisfied, skipping upgrade: pygments in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from nbconvert->notebook) (2.2.0)
Requirement already satisfied, skipping upgrade: testpath in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from nbconvert->notebook) (0.3.1)
Requirement already satisfied, skipping upgrade: mistune>=0.7.4 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from nbconvert->notebook) (0.8.3)
Requirement already satisfied, skipping upgrade: pandocfilters>=1.4.1 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from nbconvert->notebook) (1.4.2)
Requirement already satisfied, skipping upgrade: decorator in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from traitlets>=4.2.1->notebook) (4.3.0)
Requirement already satisfied, skipping upgrade: ipython>=4.0.0 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from ipykernel->notebook) (6.4.0)
Requirement already satisfied, skipping upgrade: ptyprocess; os_name != "nt" in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from terminado>=0.8.1->notebook) (0.5.2)
Requirement already satisfied, skipping upgrade: html5lib!=0.9999,!=0.99999,<0.99999999,>=0.999 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from bleach->nbconvert->notebook) (0.9999999)
Requirement already satisfied, skipping upgrade: pickleshare in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from ipython>=4.0.0->ipykernel->notebook) (0.7.4)
Requirement already satisfied, skipping upgrade: prompt-toolkit<2.0.0,>=1.0.15 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from ipython>=4.0.0->ipykernel->notebook) (1.0.15)
Requirement already satisfied, skipping upgrade: setuptools>=18.5 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from ipython>=4.0.0->ipykernel->notebook) (39.0.1)
Requirement already satisfied, skipping upgrade: backcall in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from ipython>=4.0.0->ipykernel->notebook) (0.1.0)
Requirement already satisfied, skipping upgrade: pexpect; sys_platform != "win32" in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from ipython>=4.0.0->ipykernel->notebook) (4.6.0)
Requirement already satisfied, skipping upgrade: simplegeneric>0.8 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from ipython>=4.0.0->ipykernel->notebook) (0.8.1)
Requirement already satisfied, skipping upgrade: jedi>=0.10 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from ipython>=4.0.0->ipykernel->notebook) (0.12.0)
Requirement already satisfied, skipping upgrade: wcwidth in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from prompt-toolkit<2.0.0,>=1.0.15->ipython>=4.0.0->ipykernel->notebook) (0.1.7)
Requirement already satisfied, skipping upgrade: parso>=0.2.0 in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from jedi>=0.10->ipython>=4.0.0->ipykernel->notebook) (0.2.1)
Building wheels for collected packages: vega, prometheus-client
  Running setup.py bdist_wheel for vega ... done
  Stored in directory: /root/.cache/pip/wheels/db/57/85/e0285100a71f35c73d7b3975a8899894a73c92068cd4cc1dea
  Running setup.py bdist_wheel for prometheus-client ... done
  Stored in directory: /root/.cache/pip/wheels/04/6b/a4/4ccef2edcc797b5553edc6c8bed4d219dafdb019e225c2c348
Successfully built vega prometheus-client
Installing collected packages: prometheus-client, notebook, vega
  Found existing installation: notebook 5.5.0
    Uninstalling notebook-5.5.0:
      Successfully uninstalled notebook-5.5.0
Successfully installed notebook-5.6.0 prometheus-client-0.3.1 vega-1.4.0
# load an example dataset
from vega_datasets import data
cars = data.cars()

def theme_1(*args, **kwargs):
    return {'width': 800, 'height': 150,
            'config': {'style': {'bar': {'size': 20}},
                       'legend': {'symbolSize': 20, 'titleFontSize': 20, 'labelFontSize': 20}, 
                       'axis': {'titleFontSize': 20, 'labelFontSize': 20}}}

alt.themes.register('theme_1', theme_1)
alt.themes.enable('theme_1');
# plot the dataset, referencing dataframe column names
import altair as alt
alt.renderers.enable('notebook')
alt.Chart(cars).mark_bar().encode(
  x='mean(Miles_per_Gallon)',
  y='Origin',
  color='Origin'
)

スポンサーリンク
スポンサーリンク