Reputation: 81
I am plotting a frequency histogram, and I want the x_axis to show the range of each bar, eg.[0-9],[10-19]... rather than 50, 100, 150, 200. I also want to add a trend line to this histogram like the example below. Please help!
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x=data, color_discrete_sequence=['purple'])
fig.update_layout(bargap=0.01)
fig.show()
Upvotes: 1
Views: 1259
Reputation: 1
import seaborn as sns
x = [100,300,200,500,400,300,100,700,1000,1200,3000,2000,2500,3500,4000,4500,5000,150]
sns.set_style('darkgrid')
sns.distplot(x)
Use the above code you will get a bell curve. Install the seaborn before using the above code
use the below command
pip install seaborn
Upvotes: 0
Reputation: 31236
you can go back to basics and use pandas cut()
and value_counts()
import pandas as pd
import numpy as np
import plotly.express as px
SIZE = 5
df = px.data.tips()
df = (
pd.cut(
df["total_bill"],
bins=np.arange(0, df["total_bill"].max() + SIZE, SIZE, dtype=int),
)
.value_counts()
.sort_index()
.reset_index()
.assign(label=lambda d: d["index"].apply(lambda x: f"{x.left} to {x.right}"))
)
px.bar(df, x="label", y="total_bill", color_discrete_sequence=["purple"]).update_layout(
bargap=0.01
).add_traces(
px.line(df, x="label", y="total_bill").update_traces(line={"shape": "spline"}).data
)
Upvotes: 1