Last active
November 9, 2022 14:45
-
-
Save audhiaprilliant/6f0884cf8947cd1060c99865d3d842a4 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
| # ---------- 7th ITERATION - FINAL TOUCH ---------- | |
| # 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 x-ticks | |
| plt.xticks( | |
| fontsize = 10, | |
| rotation = 0 | |
| ) | |
| # Set y-ticks | |
| plt.yticks( | |
| ticks = [], | |
| labels = [] | |
| ) | |
| # Remove left, right and top frames from matplotlib | |
| plt.gca().spines['top'].set_visible(False) | |
| plt.gca().spines['right'].set_visible(False) | |
| plt.gca().spines['left'].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 | |
| ); | |
| # Horizontal line | |
| plt.axhline( | |
| y = 2000, | |
| color = '#2c2c2c', | |
| linestyle = '--' | |
| ); | |
| # Annotation 1 | |
| plt.annotate( | |
| text = 'Q2 2022 KPI\n2,000 customers', | |
| xy = (3.15, 2050), | |
| fontsize = 9, | |
| color = '#2c2c2c', | |
| va = 'bottom', | |
| ha = 'center' | |
| ); | |
| # Annotation 2 | |
| plt.annotate( | |
| text = "In Q2 2022, customers who purchased services\nby electronic check surpass the KPI with 2,365\ncustomers in total. It's around 33.57% of total\ncustomer in Q2 2022", | |
| xy = (0.35, 2400), | |
| fontsize = 10, | |
| color = '#2c2c2c', | |
| va = 'bottom', | |
| ha = 'left', | |
| bbox = { | |
| 'facecolor': '#FFFFFF', | |
| 'alpha': 1, | |
| 'edgecolor': '#2c2c2c', | |
| 'pad': 7 | |
| } | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment