Reputation: 89
I'm trying to create a grouped bar chart in Altair with labels. Based on this post from @jakevdp grouped bar with labels. I was able to create the following chart:
This is my code:
`def missing_vals1(data, debug= False):
chart = alt.Chart(data).mark_bar(size=15).encode(
x= alt.X(field= 'claim',
type='nominal',
axis= alt.Axis(labels=False,
title= None,
ticks=False),
),
y= alt.Y('Total:Q',
axis=alt.Axis(
grid=False)
),
color = alt.Color('claim',
# header = alt.Header()
),
#column = alt.Column('claim')
)
text = chart.mark_text(
color = 'black',
dy= -5
).encode(
text = alt.Text(
'Total:Q',
format = ',.0f')
)
return alt.layer(chart, text, data=data
).facet(column = '#_missing'
).configure_view(
continuousHeight=200,
continuousWidth= 0.5
)
` However, I'd like to move the '#_missing' title as well as the numbers of each of the columns [0...14] to the bottom of the x axis, but it's not possible using the color encoding.
I also tried using the column encoding, but I wouldn't be able to use the labels (as explained in the link above).
Finally, I'd like to change the color of the text to all black and also reduce the spacing between columns.
Any tips are welcomed!
Edit: This is what the dataframe that I'm working with looks like
Upvotes: 0
Views: 1785
Reputation: 35230
Issue 1: I want to move the title and x-axis label down in the graph. This can be set in Facet in the header details.
Issue 2: I want to make the column spacing narrower. This can be done by setting the spacing in the Facet configuration.
Issue 3: I want to change the color of the text to all black. It seems that the color of the bar chart and the text cannot be changed to the same color. At the time of my research, there may be a way to do this. My experiment was to try and change the range of colors on this page to black, but both the bars and the text are black.
alt.layer(chart, text, data=data).facet(
column=alt.Column(
'#_missing:Q',
header=alt.Header(titleOrient='bottom', labelOrient='bottom'))
).configure_view(
continuousHeight=200,
continuousWidth= 0.5
).configure_facet(
spacing=0.5
)
Upvotes: 1