Bumbling around plotting two sets of seasonal data on the same chart

别来无恙 提交于 2019-12-13 03:58:36

问题


I have series of monthly inventory data since 2017. I have a series of inventory_forecasts since Dec2018

I am trying to plot the inventory data on a monthly-seasonal basis, and then overlay the inventory_forecasts of Jan2019 through Dec2019.

The dataframe looks like:

The first way I tried to make the chart does show all the data I want, but I'm unable to control the color of the inventory_zj line. Its color seems to be dominated by the color=year(date):N of the alt.Chart I configured. It is ignoring the color='green' I pass to the mark_line()

base = alt.Chart(inv.loc['2000':].reset_index(), title=f"usa total inventory").mark_line().encode(
    x='month',
    y="inventory",
    color="year(date):N"
)

#this ignores my 'green' color instruction, and marks it the same light blue 2019 color
joe = base.mark_line(color='green').encode(
    alt.Y('inventory_zj', scale=alt.Scale(zero=False), )
)

base+joe

I tried to use a layering system, but it's not working at all -- I cannot get it to display the "joe" layer

base = alt.Chart(inv.loc['2000':].reset_index(), title=f"usa total inventory").encode(
    x='month(date)'
)

doe = base.mark_line().encode(
    alt.Y('inventory', scale=alt.Scale(zero=False), ),
    color="year(date):N"
)


joe = base.mark_line(color="green").encode(
    alt.Y('inventory_zj', scale=alt.Scale(zero=False), ),
)



#looks identical to the first example
alt.layer(
    doe, joe
).resolve_scale(
    y='shared'
).configure_axisLeft(labelColor='black').configure_axisRight(labelColor='green',titleColor='green')

#independent shows a second y-axis (which is different from the left y-axis) but no line
alt.layer(
    doe, joe
).resolve_scale(
    y='independent'
).configure_axisLeft(labelColor='black').configure_axisRight(labelColor='green',titleColor='green')

I feel like i must be trying to assemble this chart in a fundamentally wrong way. I should be able to share teh same left y-axis, have the historic data colored by its year, and have a unique color for the 2019-forecasted data. But I seem to be making a mess of it.


回答1:


As mentioned in the Customizing Visualizations docs, there are multiple ways to specify things like line color, with a well-defined hierarchy: encodings override mark properties, which override top-level configurations.

In your chart, you write base.mark_point(color='green'), where base contains a color encoding which overrides the mark property. If you don't derive the layer from base (so that it does not have a color encoding), then the line will be green as you hoped. Something like this:

base = alt.Chart(inv.loc['2000':].reset_index(), title=f"usa total inventory")

inventory = base.mark_line().encode(
    x='month',
    y="inventory",
    color="year(date):N"
)

joe = base.mark_line(color='green').encode(
    x='month',
    y=alt.Y('inventory_zj', scale=alt.Scale(zero=False))
)

inventory + joe


来源:https://stackoverflow.com/questions/56661289/bumbling-around-plotting-two-sets-of-seasonal-data-on-the-same-chart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!