Created
March 7, 2022 01:04
-
-
Save audhiaprilliant/0cc68be8de51f7f5d15054c727eefcc7 to your computer and use it in GitHub Desktop.
Introduction to Plotly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 7 Highlight some points in bar | |
| # Add the colors | |
| avg_sales = df['Sales'].mean() | |
| df['Color'] = df['Sales'].apply(lambda x: '#981220' if x >= avg_sales else '#80797C') | |
| # Create object of figure | |
| fig = go.Figure() | |
| # Create a bar plot | |
| fig.add_trace( | |
| go.Bar( | |
| x = df['Fruit'], | |
| y = df['Sales'], | |
| marker_color = df['Color'], | |
| width = widths | |
| ) | |
| ) | |
| # Config for title, background, legend etc | |
| fig.update_layout( | |
| plot_bgcolor = 'white', | |
| font = { | |
| 'color': '#797979' | |
| }, | |
| xaxis = { | |
| 'title': '<b>Fruit</b>', | |
| 'linecolor': '#FFFFFF' | |
| }, | |
| yaxis = { | |
| 'title': '<b>Sales</b>', | |
| 'linecolor': '#FFFFFF' | |
| } | |
| ) | |
| # Flip the horizontal axis | |
| fig.update_xaxes( | |
| tickangle = 270 | |
| ) | |
| # Set range of vertical axis | |
| fig.update_yaxes( | |
| range = [0, 30000] | |
| ) | |
| # Add title | |
| fig.add_annotation( | |
| text = '<b><span style="color:#981220">Fruit with the most sales</span></b><span style="color:#797979"> in Fresh Fruit Store, 2021</span>', | |
| xref = 'paper', | |
| yref = 'paper', | |
| x = -0.095, | |
| y = 1.325, | |
| showarrow = False, | |
| align = 'left', | |
| xanchor = 'left', | |
| font = { | |
| 'size': 18, | |
| 'color': '#242526' | |
| } | |
| ) | |
| # Add subtitle | |
| fig.add_annotation( | |
| text = 'Data is from 5th January to 24th December 2021', | |
| xref = 'paper', | |
| yref = 'paper', | |
| x = -0.095, | |
| y = 1.24, | |
| showarrow = False, | |
| align = 'left', | |
| xanchor = 'left', | |
| font = { | |
| 'size': 11 | |
| } | |
| ) | |
| # Add description | |
| fig.add_annotation( | |
| text = "In 2021, bananas become the most favorite fruit, as 25,750 units were sold or about 18.9%. The number of sales for bananas and apples are<br>quite gaping, where 15,750 apples were sold. The rest of fruits sold at 10,000 to 14,500 units in 2021 with an average of around 11,800 units", | |
| xref = 'paper', | |
| yref = 'paper', | |
| x = -0.095, | |
| y = 1.175, | |
| showarrow = False, | |
| align = 'left', | |
| xanchor = 'left', | |
| font = { | |
| 'size': 13, | |
| 'color': '#242526' | |
| } | |
| ) | |
| # Add highlight text | |
| fig.add_annotation( | |
| text = 'Almost 51.2% of total sales in<br>2021 comes from big four fruits', | |
| xref = 'paper', | |
| yref = 'paper', | |
| x = 0.1, | |
| y = 0.7, | |
| showarrow = False, | |
| align = 'center', | |
| xanchor = 'left', | |
| font = { | |
| 'size': 13, | |
| 'color': '#242526' | |
| } | |
| ) | |
| # Show graph | |
| fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment