Why do I have extra values on X-axis, with bar plot?

627 Views Asked by At

There are only [3,4,5,6,8] values for Cylinders in Auto.csv, but when I created a bar plot using plotly, I see that there is one extra value for Cylinder over X-axis which is 7.

How to eliminate that additional point?

Thanks in advance.

Following is the screenshot of the code executed

1

There are 1 best solutions below

0
On

You could change your axis type to categorical which prevents Plotly from filling your "missing" values.

fig.update_xaxes(type='category')

enter image description here

import pandas as pd
import plotly.express as px

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/mtcars.csv')
df_mean = df[['cyl', 'mpg']].groupby('cyl').mean().reset_index()

fig = px.bar(df_mean, 'cyl', 'mpg')
fig.update_xaxes(type='category')
fig.show()