As discussed in previous articles, data visualization is essential to clearly showing data insights. In this new article, we’ll explore different ways of visualizing your data with Python. You’ll see that there is much more than just the simple horizontal bar plot.

Set Up & Connect to the Database
To connect to the Database and Retrieve the query you want to visualize, checkout my other article:
Boring Data? Visualize it!
1. Bar Plot
A bar plot is a straightforward way to visualize the total amount spent by the top customers.
import seaborn as sns
import matplotlib.pyplot as plt
# Set seaborn style
sns.set_style("whitegrid")
# Create a bar plot of total amount spent by top customers
plt.figure(figsize=(10, 6))
barplot = sns.barplot(data=df, x='first_name', y='total_amount', palette="viridis")
plt.title('Top 10 Customers by Total Amount Spent', fontsize=16)
plt.xlabel('Customers', fontsize=14)
plt.ylabel('Total Amount Spent', fontsize=14)
plt.xticks(fontsize=12, rotation=45)
plt.yticks(fontsize=12)
plt.tight_layout()
# Add labels above the bars
for p in barplot.patches:
height = p.get_height()
barplot.annotate(f'{height:.0f}',
xy=(p.get_x() + p.get_width() / 2., height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='center')
plt.show()

2. Pie Chart
A pie chart can show the proportion of the total amount spent by each customer.
plt.figure(figsize=(8, 8))
plt.pie(df['total_amount'], labels=df['first_name'], autopct='%1.1f%%', colors=sns.color_palette("viridis", len(df)))
plt.title('Proportion of Total Amount Spent by Top Customers')
plt.show()

3. Horizontal Bar Plot
A horizontal bar plot can be an alternative to the vertical bar plot, making it easier to read long customer names.
plt.figure(figsize=(10, 6))
barplot = sns.barplot(data=df, x='total_amount', y='first_name', palette="viridis")
plt.title('Top Customers by Total Amount Spent', fontsize=16)
plt.xlabel('Total Amount Spent', fontsize=14)
plt.ylabel('Customers', fontsize=14)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.tight_layout()
# Add labels to the bars
for p in barplot.patches:
width = p.get_width()
barplot.annotate(f'{width:.0f}',
xy=(width, p.get_y() + p.get_height() / 2),
xytext=(5, 0), # 5 points horizontal offset
textcoords="offset points",
ha='left', va='center')
plt.show()

4. Box Plot
A box plot can provide insights into the distribution of the total amounts spent by customers.
plt.figure(figsize=(10, 6))
sns.boxplot(data=df, x='total_amount')
plt.title('Distribution of Total Amount Spent by Top Customers', fontsize=16)
plt.xlabel('Total Amount Spent', fontsize=14)
plt.tight_layout()
plt.show()

5. Violin Plot
A violin plot is similar to a box plot but provides additional information about the density of the data at different values.
plt.figure(figsize=(10, 6))
sns.violinplot(data=df, x='total_amount')
plt.title('Distribution of Total Amount Spent by Top Customers', fontsize=16)
plt.xlabel('Total Amount Spent', fontsize=14)
plt.tight_layout()
plt.show()

6. Scatter Plot
If you have another variable to compare against the total amount spent, a scatter plot can be very useful. For instance, if you had data on the number of transactions per customer, you could visualize the relationship between the number of transactions and the total amount spent.
plt.figure(figsize=(10, 6))
scatterplot = sns.scatterplot(data=df, x='total_amount', y='num_transactions', hue='first_name', palette="viridis", s=100)
plt.title('Total Amount Spent vs Number of Transactions', fontsize=16)
plt.xlabel('Total Amount Spent', fontsize=14)
plt.ylabel('Number of Transactions', fontsize=14)
plt.legend(title='Customers', bbox_to_anchor=(1.05, 1), loc='upper left')
plt.tight_layout()
plt.show()

Recap

There’s more than one way of visualizing your data. Try different styles and charts to keep your audience engaged with your reports. Mixing your reports with other types of graphs and adjusting them accordingly depending on the kind of information you want to show can make it easier to show your key points and highlight important data.
Thank you for reading! If you enjoyed this article, please consider supporting me in the following ways:
- Clap for this story 👏 as often as you like, or Leave a comment 💬 to share your thoughts and feedback.
- Follow me on Medium to stay updated with my latest articles.
- Follow me on X for daily programming tips and more!
- Buy me a coffee ☕️.
- Visit My Digital Store for PostgreSQL Tips, Ebooks, Excel Templates and more!
Your support means the world to me. Thank you!







Leave a reply to adriyanrogue Cancel reply