Last active
November 9, 2022 14:44
-
-
Save audhiaprilliant/195af993ab127125ac0bc26c42136a39 to your computer and use it in GitHub Desktop.
Matplotlib 101 - Basic Introduction for Python Beginner
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
| # ---------- 6th ITERATION - ADD ANOTHER DATA LABELS ---------- | |
| # Figure size | |
| fig = plt.figure(figsize = (10, 4.8)) | |
| # Bar plot | |
| bar_fig = plt.bar( | |
| x = 'PaymentMethod', | |
| height = 'customerID', | |
| data = df_group_1, | |
| width = 0.5 | |
| ); | |
| # Set colors | |
| colors = ['#981220', '#80797C', '#80797C', '#80797C'] | |
| for i in range(len(colors)): | |
| bar_fig[i].set_color(colors[i]) | |
| # Set y-Limit | |
| plt.ylim([0, 3000]); | |
| # Set y-ticks | |
| plt.yticks( | |
| ticks = range(0, 3000, 500), | |
| labels = range(0, 3000, 500) | |
| ) | |
| # Remove right & top frames from matplotlib | |
| plt.gca().spines['top'].set_visible(False) | |
| plt.gca().spines['right'].set_visible(False) | |
| # Add values | |
| for bar in bar_fig: | |
| plt.annotate( | |
| text = bar.get_height(), | |
| xy = ( | |
| bar.get_x() + 0.14, | |
| bar.get_height() + 50), | |
| fontsize = 12 | |
| ) | |
| # Plot title | |
| plt.title( | |
| label = 'Total customer by payment method', | |
| loc = 'left', | |
| y = 1.1, | |
| fontsize = 16, | |
| fontweight = 'bold', | |
| color = '#981220' | |
| ); | |
| # Plot subtitle | |
| plt.suptitle( | |
| t = 'Telecommunication Customer Churn 2022', | |
| x = 0.3, | |
| y = 0.95, | |
| fontsize = 12, | |
| fontweight = None | |
| ); | |
| # Vertical axis label | |
| plt.ylabel( | |
| ylabel = 'Total customer', | |
| fontsize = 12, | |
| fontweight = 'bold', | |
| color = '#2c2c2c' | |
| ); | |
| # Horizontal axis label | |
| plt.xlabel( | |
| xlabel = 'Payment method', | |
| fontsize = 12, | |
| fontweight = 'bold', | |
| color = '#2c2c2c' | |
| ); | |
| # Horizontal line | |
| plt.axhline( | |
| y = 2000, | |
| color = '#2c2c2c', | |
| linestyle = '--' | |
| ); | |
| # Annotation 1 | |
| plt.annotate( | |
| text = 'Q2 2022 KPI: 2,000 customers', | |
| xy = (2.35, 2100), | |
| fontsize = 10, | |
| color = '#2c2c2c', | |
| ); | |
| # Annotation 2 | |
| plt.annotate( | |
| text = 'In Q2 2022, customers who purchased services\nby electronic check surpass the KPI with 2,365\ncustomers in total', | |
| xy = (0, 2100), | |
| xytext = (0.5, 2500), | |
| fontsize = 10, | |
| color = '#2c2c2c', | |
| arrowprops = { | |
| 'arrowstyle': '-', | |
| 'color': '#2c2c2c', | |
| 'linewidth': 1 | |
| } | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment