'Is it possible to customize the size of marks in Altair?
I want to change the size of the squares so that the contrast between the larger ones is easier to see, and the initial first two don't have such a big gap. Is there a way to do this? No matter what I put into the range option in the code below, I get less than ideal results.
sun_square_plot = alt.Chart(climate_with_temps).mark_square().encode(
alt.X('month:O', axis=alt.Axis(title="Month"), sort=list(set(climate_with_temps.city))),
alt.Y('avg_temp:Q', axis=alt.Axis(title="Average Temperature in Fahrenheit"), scale=alt.Scale(domain=[30, 95])),
alt.Color('city:N', scale=alt.Scale(scheme='dark2'), legend=alt.Legend(title='City')),
alt.Size('sun_bin:O', sort=['0 - 50', '50 - 99', '100 - 149', '150 - 199', '200 - 249', '250 - 299', '300 - 350'], legend=alt.Legend(title='Average Hours of Sunshine'),
scale=alt.Scale(range=[20, 1000])),
alt.OpacityValue(0.75)
).properties(
height=500,
width=700
)
Solution 1:[1]
Sizes work like other encodings in Altair, so you can think of this the same as if you would have the variable spread out over your x or y-axis and wanting to change the distance between the small values to be smaller and the big values to be larger. You could raise all the numbers to an exponent to rearrange them this way, which is the same as change the type of the scale to be exponential (same principle as when we use a log scale, but that is for decreasing the distance of large numbers). So you could try alt.Size(scale=alt.Scale(type='pow'))
.
I believe it should also work if you set the domain
to exclude the smallest value. If you provide a sample data set pasted as text in your question (or use one from the altair gallery), you can get more detailed help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | joelostblom |