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:
my question: is there any possible way to suppress this plot legend in the graph output?
You can remove the legend entirely by submitting a null value.
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.
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.
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.
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.
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.
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:
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),
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With