I have following dataframe:
data = {'make' : ['Audi', 'Audi', 'BMW', 'Mercedes', 'Mercedes'],
'model':["A4", "R8", "M4", "AMG GT", "SLS"],
'REF':['GT2', 'GT3', 'GT4', 'GT5','GT6'],
'weight':[1460, 1260, 1280, 1800, 1600]}
df = pd.DataFrame(data)
df
and I need to plot the weight for each vehicle as a bar chart in plotly express.
fig = px.bar(
df,
x='REF',
y='weight',
barmode='group',
template = "plotly_dark",
text="final weight")
Now, I would like to change the automatic xlabels (GT2, GT3, etc) by concatenating columns make and model to have instead of GT2 -> Audi A4
Any suggestions how I can achieve this?
Thank you!
I believe you can use update_xaxes
:
fig.update_xaxes(tickangle = -45,
tickmode = 'array',
tickvals = list(range(len(df))),
ticktext = df['make'] + ' ' + df['model'])
Output:
EDIT:
Better idea:
The following answer is one way to do this, but it is also possible to use Graph Update to update the x-axis tick labels in a batch.
fig.update_traces(x=[x[0]+' '+x[1] for x in df[['make','model']].values])
- r-beginners Apr 27 at 0:16