In this particular Assignment we were assigned to recreate some plots suggested by our Professor using plotly Express. The dataset that we will using is "tips" data set from plotly express library.
Let's import plotly express and tips data set from plotly express.
!pip install --upgrade plotly
Requirement already satisfied: plotly in /Users/waheebalgabri/anaconda3/lib/python3.10/site-packages (5.9.0)
Collecting plotly
Downloading plotly-5.18.0-py3-none-any.whl (15.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 15.6/15.6 MB 14.9 MB/s eta 0:00:0000:0100:01
Requirement already satisfied: tenacity>=6.2.0 in /Users/waheebalgabri/anaconda3/lib/python3.10/site-packages (from plotly) (8.0.1)
Requirement already satisfied: packaging in /Users/waheebalgabri/anaconda3/lib/python3.10/site-packages (from plotly) (22.0)
Installing collected packages: plotly
Attempting uninstall: plotly
Found existing installation: plotly 5.9.0
Uninstalling plotly-5.9.0:
Successfully uninstalled plotly-5.9.0
Successfully installed plotly-5.18.0
import plotly.express as px
df = px.data.tips()
Lets display the first few rows to see if the data set got loaded properly.
df.head()
| total_bill | tip | sex | smoker | day | time | size | |
|---|---|---|---|---|---|---|---|
| 0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
As we can see that the data set looks good now we can recreate the required graphs
fig = px.bar(data_frame=df, x="sex", y="total_bill", color = "smoker",barmode='group')
fig.show()
fig = px.bar(data_frame=df, x="sex", y="total_bill", color = "day",barmode='group')
fig.show()
fig = px.scatter(data_frame=df, x="total_bill", y="tip", color = "sex",facet_col='smoker')
fig.show()
fig = px.scatter(data_frame=df, x="total_bill", y="tip", color = "sex",facet_col='smoker', trendline="ols")
fig.show()
fig = px.scatter(data_frame=df, x="total_bill", y="tip", color = "sex",facet_row='time',facet_col='day',category_orders={'day': ['Thur', 'Fri', 'Sat', 'Sun'], 'time': ['Dinner', 'Lunch']})
fig.show()
fig = px.histogram(data_frame=df, x="tip", marginal="rug")
fig.show()
fig = px.box(data_frame=df, x="smoker", y="total_bill", color = "smoker")
fig.show()