[Python] Display the Altair legend in the plot area

How To Place Legend Inside the Plot area in Altair

About this article

Recently, I like Altair, which is compatible with pandas/dataframe. However, the position of the legend is displayed in the plot with matplotlib, but it is outside with Altair, so I did not know how to adjust the position. I couldn't find the material in English, but I solved it by looking at the Vega-Lite document parsed by Altair, so I will write it as an article.

I would like to explain with a graph of Iris data from this site, which was very helpful. [Recommendation of Altair! Data visualization with Python] (https://qiita.com/dr666m1/items/b71bef31ba3a21868095)

BEFORE By default, it will appear on the right side スクリーンショット 2021-01-19 100349.png

import altair as alt
from vega_datasets import data
iris = data.iris()

alt.Chart(iris).mark_point().encode(
    alt.X("sepalLength", scale=alt.Scale(zero=False)),
    alt.Y("sepalWidth", scale=alt.Scale(zero=False)),
    color="species"
).interactive()

AFTER can be moved to any location in the plot area スクリーンショット 2021-01-19 101032.png


import altair as alt
from vega_datasets import data
iris = data.iris()

alt.Chart(iris).mark_point().encode(
    alt.X("sepalLength", scale=alt.Scale(zero=False)),
    alt.Y("sepalWidth", scale=alt.Scale(zero=False)),
    color=alt.Color("species", 
        legend=alt.Legend(orient='none', legendX=250, legendY=5,  ##If orient is set to none, legendX,legendY can be specified
                strokeColor='blue', fillColor='white', padding=10)) ##Legend square setting
)

Legend setting points

As you can see from the code, you can make various fine adjustments with alt.Legend. You can adjust the position with LegendX and LegendY, but the point is to set oriend ='none' at that time. In addition, I do not know what the numerical values ​​of x and y are ... I can do it somehow, but if anyone knows, please let me know.

Below, I will take a screenshot of the important parts of the Veg-Lite document that I referred to. Legend | Vega-Lite https://vega.github.io/vega-lite/docs/legend.html

スクリーンショット 2021-01-19 103539.png スクリーンショット 2021-01-19 103610.png スクリーンショット 2021-01-19 103720.png


Although it has nothing to do with this article, the examples of this Chinese site were substantial. Python Altair COVID-19 Epidemic Number Visualization Practice Economic Experience https://www.cnblogs.com/dotlink/archive/2004/01/13/covid-19.html

Recommended Posts

[Python] Display the Altair legend in the plot area
Display Python 3 in the browser with MAMP
How to display legend marks in one with Python 2D plot
Enlarge the plot display.
I want to display the progress in Python!
Display n digits after the decimal point in python
Plot and understand the multivariate normal distribution in Python
Python OpenCV tried to display the image in text.
Download the file in Python
Find the difference in Python
Display UTM-30LX data in Python
Plot geographic information in Python
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
Python in the browser: Brython's recommendation
Save the binary file in Python
Hit the Sesami API in Python
Get the desktop path in Python
Get the script path in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
Display characters like AA in python
Hit the web API in Python
I wrote the queue in Python
Calculate the previous month in Python
Examine the object's class in python
Get the desktop path in Python
Get the host name in Python
Rectangle area element split in Python
Access the Twitter API in Python
The first step in Python Matplotlib
I wrote the stack in Python
Master the weakref module in Python
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
python> Display 3 decimals in ".3f, .3f, .3f" format / Get 3 coordinate values in the range [-1: 1]
Feel free to change the label of the legend in Seaborn in python
[Cloudian # 2] Try to display the object storage bucket in Python (boto3)
How to display bytes in the same way in Java and Python
Display LaTeX notation formulas in Python, matplotlib
Learn the design pattern "Prototype" in Python
Learn the design pattern "Builder" in Python
[Python, Julia] 3D display in Jupyter-Mayavi library
Try using the Wunderlist API in Python
Check the behavior of destructor in Python
Learn the design pattern "Flyweight" in Python
Django ~ Let's display it in the browser ~
Try using the Kraken API in Python
Learn the design pattern "Observer" in Python
Learn the design pattern "Memento" in Python
Learn the design pattern "Proxy" in Python
Write the test in a python docstring
Display a list of alphabets in Python 3
Learn the design pattern "Command" in Python
OR the List in Python (zip function)
Tweet using the Twitter API in Python
Learn the design pattern "Bridge" in Python
Check if the URL exists in Python
Learn the design pattern "Mediator" in Python
Associate the table set in python models.py
The result of installing python in Anaconda
How to display multiplication table in python
How to extract polygon area in Python