简体   繁体   中英

How to adjust the scale of a multigraph (Altair) and add a legend?

My code:

    size = alt.datum.confirmed > 0

    testplot1 = alt.Chart(df_ww_ru).transform_filter(
        size
    ).mark_line(color='red').encode(
        x=alt.X('date:T', type='temporal', title='Дата'),
        y=alt.Y('sum(confirmed):Q',  title='aboba'),
        tooltip='sum(confirmed)'
    )

    testplot2 = alt.Chart(df_ru_ru).transform_filter(
        size
    ).mark_line(color='blue').encode(
        x=alt.X('date:T', type='temporal', title='Дата'),
        y=alt.Y('sum(confirmed):Q', title='aboba'),
        tooltip='sum(confirmed)'
    )

    testplot_rez = alt.layer(testplot1, testplot2).configure_view(
    ).properties(
        width=820,
        height=500
    ).configure_axis(
        labelFontSize=17,
        titleFontSize=20
    )

As a result, I get such a graph with a completely gigantic scale. When I display a non-combined schedule, everything is fine.

在此处输入图像描述

How can this be fixed? I also need to add a legend where it will be necessary to sign these two lines, how to do it? Is it possible to do it in a similar way as in matplotlib? That is, name everything manually.

In terms of adding the legend, the easiest would probably be to combine your two dataframes so that there is one dataframe with the columns date , confirmed , and category . The values of category would be either wu_ru or ru_ru . This way you can set the encoding color='category' when you create the altair plot and have the legend created automatically as in this example . You can probably use pandas merge or concat to combine the dfs and then pandas melt to get it into the right format. More info about data formats in Altair can be found in the docs .

In terms of the y-axis extent, it is automatically adjusted to what is in the data, so I would have a look at the values you have there and make sure that there are no really large ones that throw off the axis. If you want to change the axis scale manually you can use something like alt.Y('sum(confirmed):Q', scale=alt.Scale(domain=0, 1000))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM