Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

altair remove or suppress automatically generated plot legend

When using the altair package I've noticed that when a chart is created a plot legend is also generated. The follow code:

import altair as alt
from vega_datasets import data
alt.renderers.enable('notebook')

cars = data.cars()

alt.Chart(cars).mark_circle().encode(x='Horsepower', 
                                     y='Miles_per_Gallon',
                                     color='Origin',
                                     tooltip=['Name', 'Origin', 'Horsepower', 'Miles_per_Gallon']).interactive()

produces this graph:

altair graph

my question: is there any possible way to suppress this plot legend in the graph output?

like image 494
James Draper Avatar asked Oct 30 '18 15:10

James Draper


People also ask

How to remove legend in altair?

You can remove the legend entirely by submitting a null value.

How do I remove the grid from Altair?

We can remove the grid lines on x or y-axis by specifying the argument grid=False inside alt. X() or alt. Y() method in the encoding channels.

How do I change the color of my Altair chart?

Customizing Colors If you don't like the colors chosen by Altair for your scatter plot, you can customize the colors. The default colors can be changed using the scale argument of the Color class, By passing the Scale class to the scale argument.

How do I remove a legend from an Altair chart?

One simple example is giving the legend a title. Another thing you can do is move the legend to another position with the orient argument. You can remove the legend entirely by submitting a null value. Basic Altair charts are drawn with both a grid and an outside border.

How to create a chart with no border in Altair?

Basic Altair charts are drawn with both a grid and an outside border. To create a chart with no border, you will need to remove them both. As an example, let’s start with a simple scatter plot. First remove the grid using the Chart.configure_axis () method.

How does Altair Choose the default color scheme?

As discussed in Effect of Data Type on Color Scales, Altair chooses a suitable default color scheme based on the type of the data that the color encodes. These defaults can be customized using the scale argument of the Color class.

How to “clamp” data in Altair?

The problem is that the data still exists beyond the scale, and we need to tell Altair what to do with this data. One option is to “clip” the data by setting the "clip" property of the mark to True: Another option is to “clamp” the data; that is, to move points beyond the limit to the edge of the domain:


Video Answer


1 Answers

There is an example for this in the documentation for the altair module. You can find it here.

Here they set the Legend to None, which removes the legend.

Here is their example-code:

import altair as alt
from vega_datasets import data

iris = data.iris()

alt.Chart(iris).mark_point().encode(
    x='petalWidth',
    y='petalLength',
    color=alt.Color('species', legend=None),
)
like image 186
Hampus Larsson Avatar answered Oct 08 '22 18:10

Hampus Larsson