Last active
November 9, 2022 14:43
-
-
Save audhiaprilliant/da9a44b7424a4c3e8239246d6b4aff3c 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
| # ---------- 4th ITERATIONS - ADD DATA LABELS AND TITLES ---------- | |
| # 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 left, right and top frames | |
| 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', | |
| x = 0.275, | |
| 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' | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment