
Are you adding data visualization in Python? Data visualization is the cornerstone of effective data analysis. Whether you’re exploring datasets, presenting findings, or building dashboards, Matplotlib provides the foundation for creating compelling visualizations in Python. In this comprehensive guide, we’ll explore essential chart types and customization techniques using a real employee dataset.
Data Visualization in Python – Common Charts
Scatter Plot
Scatter plots are perfect for identifying relationships between two continuous variables.
Import matplotlib and numpy. This are the tools you will need to create the charts.
import matplotlib.pyplot as plt
import numpy as np
x_data = np.random.random(1000) * 100
y_data = np.random.random(1000) * 100
plt.scatter(x_data, y_data, c = ‘red’, marker = ‘*’, s = 150, alpha = 0.3 )
plt.show() This specifies the scatter plot as star and the size and shape of the star

Line Graph
Line graphs excel at showing trends and changes over time.
The code specifies the line color as red and the line style as a broken line.
weight = [80,83,84,81,83,79,75,80,85,80,81,82,82,89,88,87]
plt.plot(year, weight, c = ‘red’, marker = ‘.’, lw=3, linestyle = ‘–‘)
plt.show()

Bar Chart
Bar charts are ideal for comparing categories and showing distributions.
The ugly in this chart is deliberate! Its just meant to show that you can customize your chart as much as you want to. In this chart the color is specified as red, width 0.5, edge color blue and the line weight of the edge color is 3.
x = [‘C++’, ‘C#’, ‘Python’, ‘Java’, ‘Go’]
y = [20, 50, 140, 1, 45]
plt.bar(x,y, color =’red’, width = 0.5, edgecolor = ‘b’, lw = 3)
plt.show()

Histogram
Histograms reveal the distribution and frequency of continuous data.
This code specifies 20 bins where all the ages are divided into so categories.
ages = np.random.normal(20,1.5,1000) plt.hist(ages,
bins = 20,
cumulative=True)

Pie Chart
Pie charts effectively show proportions and percentages of a whole.
Explodes is used to highlight something, in this case C
langs = [‘Python’, ‘C++’, ‘Java’, ‘C#’, ‘Go’]
votes = [50, 24,14,6,17]
explodes = [0,0,0,0.3,0]
plt.pie(votes, labels = langs,
explode=explodes,
autopct = “%.1f%%”,
startangle = 90)
plt.show()

Box Plot
Box plots reveal distribution, outliers, and quartiles in your data.
second = np.linspace(10,200,25)
third = np.linspace(200,210,25)
fourth = np.linspace(210,230,25)
data = np.concatenate((first, second, third, fourth))
plt.boxplot(data)
plt.show()

Data Visualization in Python – Customization of Axis and Titles
Customizing your charts, ensuring you have given descriptive titles and labeled each axis makes them easier to understand.
In this chart, the title, the X and Y axis are well defined, the font and font size is also specified.
years = [2014, 2015, 2016, 2017,
2018, 2019, 2020, 2021]
Income = [55, 56, 62,61,72,72, 73, 75]
income_ticks = list(range(50,81,2))
plt.plot(years, Income)
plt.title(‘Yearly Income of John(in USD)’, fontsize = 18, fontfamily = ‘Acme’)
plt.xlabel(‘Year’, fontsize = 15, fontfamily = ‘Acme’),
plt.ylabel(‘Income in USD’, fontsize = 15, fontfamily = ‘Acme’)
plt.yticks(income_ticks, [f”{x}K ” for x in income_ticks], fontsize = 12, fontfamily = ‘Acme’)
plt.show()

This chart combines 3 line charts as also shows a legend. You can specify the position of the legend.
stock_a = [100,102,99,101,101,100,102]
stock_b = [90,95,102,104,105,103,109]
stock_c = [110,115,100,105,100,98,95]
plt.plot(stock_a, label = ‘Company1’)
plt.plot(stock_b, label = ‘Company2’)
plt.plot(stock_c, label = ‘Company3’)
plt.legend(loc = “lower center”)
plt.show()

This is another example of how to add a labels and legends to your charts.
votes = [10, 2, 5, 16, 22]
people = [‘A’,’B’,’C’,’D’,’E’]
plt.pie(votes, labels = None)
plt.legend(labels = ‘people’)
plt.show()

This one is very interesting. You can actually import a chart style and use it.
from matplotlib.pyplot import style
style.use(‘dark_background’)
votes = [10, 2, 5, 16, 22]
people = [‘A’,’B’,’C’,’D’,’E’]
plt.pie(votes, labels = None)
plt.legend(labels = ‘people’)
plt.show()

Conclusion
Matplotlib is an incredibly powerful tool for data visualization in Python. From basic plots to sophisticated dashboards, mastering these chart types and customization techniques will significantly enhance your data analysis capabilities.
Key Takeaways:
- Choose the right chart type for your data story
- Always customize titles, labels, and colors for clarity
- Use multiple plots to provide comprehensive insights
- Practice with real datasets to build proficiency
Start experimenting with these examples using your own datasets, and remember that great visualizations tell compelling stories about your data!