Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust scale ranges in altair?

I'm having trouble getting all of the axes onto the same scale when using altair to make a group of plots like so:

class_list = ['c-CS-m','c-CS-s','c-SC-m','c-SC-s','t-CS-m','t-CS-s','t-SC-m','t-SC-s']
list_of_plots = []

for class_name in class_list:
    list_of_plots.append(alt.Chart(data[data['class'] == class_name]).mark_bar().encode(
    x = alt.X('DYRK1A', bin = True, scale=alt.Scale()),
    y = 'count()').resolve_scale(
    y='independent'
))

list_of_plots[0] & list_of_plots[1] | list_of_plots[2] & list_of_plots[3] | list_of_plots[4] & list_of_plots[5] | list_of_plots[6] & list_of_plots[7]

I'd like to have the x axis run from 0.0 to 1.4 and the y axis run from 0 to 120 so that all eight plots I'm producing are on the same scale! I've tried to use domain, inside the currently empty Scale() call but it seems to result in the visualisations that have x axis data from say 0.0 to 0.3 being super squished up and I can't understand why?

For context, I'm trying to plot continuous values for protein expression levels. The 8 plots are for different classes of mice that have been exposed to different conditions. The data is available at this link if that helps: https://archive.ics.uci.edu/ml/datasets/Mice+Protein+Expression

Please let me know if I need to provide some more info in order for you to help me!

like image 325
Jaimee-lee Lincoln Avatar asked Jan 24 '23 23:01

Jaimee-lee Lincoln


1 Answers

First of all, it looks like you're trying to create a wrapped facet chart. Rather than doing that manually with concatenation, it's better to use a wrapped facet encoding.

Second, when you specify resolve_scale(y='independent'), you're specifying that the y-scales should not match between subcharts. If instead you want all scales to be shared, you can use resolve_scale(y='shared'), or equivalently just leave that out, as it is the default.

To specify explicit axis domains, use alt.Scale(domain=[min, max]). Put together, it might look something like this:

alt.Chart(data).mark_bar().encode(
    x = alt.X('DYRK1A', bin = True, scale=alt.Scale(domain=[0, 1.4])),
    y = alt.Y('count()', scale=alt.Scale(domain=[0, 120]),
    facet = alt.Facet('class:N', columns=4),
)
like image 114
jakevdp Avatar answered Jan 28 '23 11:01

jakevdp