Reputation: 187
I have a Pandas dataframe that looks like this:
| date_column | my_point_column | my_line_column |
|---------------------|-------------------|------------------|
| 2017-12-13 00:00:00 | 3 | 9.73136 |
| 2018-01-05 00:00:00 | 23 | 6.36568 |
| 2018-01-11 00:00:00 | 6 | 11.9105 |
| 2018-01-22 00:00:00 | 11 | 10.4328 |
| ... | ... | ... |
I want to plot both series using different marks (points and lines) and they should each be a different color. The color also must be shown in the legend.
I was able to accomplish it with this code, but it's pretty verbose:
# plot_df is the dataframe shown above
chart = alt.Chart(plot_df)
points = chart.transform_fold(
fold=["my_point_column"],
as_=["variable", "value"]
).encode(
x="date_column:T",
y="my_point_column:Q",
color="variable:N"
).mark_circle()
line = chart.transform_fold(
fold=["my_line_column"],
as_=["variable", "value"]
).encode(
x="date_column:T",
y="my_line_column:Q",
color="variable:N"
).mark_line()
alt.layer(points, line)
That code produces this chart:
That's pretty much the result I'm looking for (outside of customizing the colors, axis titles, etc.). Is there an easier way to do it without having to use transform_fold twice?
Upvotes: 1
Views: 1362
Reputation: 48879
For a less verbose solution, you could use one of the charts as the "base" and then overwrite its transform, mark, and y-encoding:
points = alt.Chart(plot_df).mark_circle().transform_fold(
fold=["my_point_column"]
).encode(
x="date_column:T",
y="my_point_column:Q",
color="key:N")
line = chart.mark_line().transform_fold(
fold=["my_line_column"]
).encode(
y="my_line_column:Q")
points + line
Upvotes: 1