- CodeCraft by Dr. Christine Lee
- Posts
- Say Goodbye to Boring Data
Say Goodbye to Boring Data
Create 5 Eye-Catching Charts with Matplotlib

Introduction to Python Matplotlib for Beginners
Welcome to the world of data visualisation with Matplotlib! If you're new to Python and data visualisation, this post is designed just for you. We'll introduce you to Matplotlib, explain what visualisation is, and its purpose and importance. Plus, we'll dive into some key functions and parameters, providing real-life examples to help you draw different charts.
|
What is Data Visualisation?
Data visualisation is the graphical representation of information and data. By using visual elements like charts, graphs, and maps, data visualisation tools provide an accessible way to see and understand trends, outliers, and patterns in data.
Why is Data Visualisation Important?
Simplifies Complex Data: Visualisations make it easier to understand complex data.
Identifies Trends and Patterns: Helps in spotting trends and patterns that may not be obvious in raw data.
Facilitates Decision-Making: Provides insights that can lead to informed decisions.
Communicates Information Effectively: Visual representations can communicate findings more effectively than text or tables.
Introduction to Matplotlib
Matplotlib is a powerful and versatile data visualisation library in Python. It is widely used for creating static, interactive, and animated visualisations. Whether you're a beginner or an experienced data scientist, Matplotlib has the tools you need to create stunning visualisations.
Key Functions in Matplotlib
Let's explore some of the key functions in Matplotlib and their parameters.
1. matplotlib.pyplot.plot()
The plot()
function is used to create a simple line chart.
Parameters:
- x
: The data for the x-axis.
- y
: The data for the y-axis.
- label
: Label for the line (optional).
- color
: Color of the line (optional).
- linestyle
: Style of the line (optional).
Example
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [10, 15, 13, 20, 25]
# Create a simple line chart
plt.plot(x, y, label='Sales', color='blue', linestyle='--')
# Add title and labels
plt.title('Monthly Sales')
plt.xlabel('Month')
plt.ylabel('Sales')
# Show legend
plt.legend()
# Display the chart
plt.show()

2. matplotlib.pyplot.bar()
The bar()
function is used to create a bar chart.
Parameters:
- x
: The categories.
- height
: The heights of the bars.
- width
: The width of the bars (optional).
- color
: Color of the bars (optional).
Example:
import matplotlib.pyplot as plt
# Data
categories = ['A', 'B', 'C', 'D', 'E']
values = [10, 15, 7, 10, 5]
# Create a bar chart
plt.bar(categories, values, color='green')
# Add title and labels
plt.title('Category Values')
plt.xlabel('Categories')
plt.ylabel('Values')
# Display the chart
plt.show()

3. matplotlib.pyplot.scatter()
The scatter()
function is used to create a scatter plot.
Parameters:
- x
: The data for the x-axis.
- y
: The data for the y-axis.
- color
: Color of the points (optional).
- marker
: Style of the markers (optional).
Example:
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 40]
# Create a scatter plot
plt.scatter(x, y, color='red', marker='o')
# Add title and labels
plt.title('Scatter Plot Example')
plt.xlabel('X values')
plt.ylabel('Y values')
# Display the chart
plt.show()

4. matplotlib.pyplot.hist()
The hist()
function is used to create a histogram.
Parameters:
- x
: The data to be binned.
- bins
: The number of bins (optional).
- color
: Color of the bars (optional).
Example:
import matplotlib.pyplot as plt
# Data
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
# Create a histogram
plt.hist(data, bins=5, color='purple')
# Add title and labels
plt.title('Histogram Example')
plt.xlabel('Value')
plt.ylabel('Frequency')
# Display the chart
plt.show()

5. matplotlib.pyplot.pie()
The pie()
function is used to create a pie chart.
Parameters:
- x
: The data for the wedges.
- labels
: A sequence of strings providing the labels for each wedge (optional).
- colors
: A sequence of colors for each wedge (optional).
Example:
import matplotlib.pyplot as plt
# Data
sizes = [25, 35, 20, 20]
labels = ['A', 'B', 'C', 'D']
# Create a pie chart
plt.pie(sizes, labels=labels, colors=['blue', 'green', 'red', 'yellow'], autopct='%1.1f%%')
# Add title
plt.title('Pie Chart Example')
# Display the chart
plt.show()

The Ultimate Guide to Choosing the Right Chart for Your Data
Chart Type | Description | When to Use | Example |
---|---|---|---|
Line Chart | A graph that uses lines to connect individual data points. | Use when you want to show trends over time or continuous data. | Tracking monthly sales figures over a year. |
Bar Chart | A chart that represents categorical data with rectangular bars. | Use to compare quantities of different categories or to show changes over time. | Comparing sales of different products in a store. |
Scatter Plot | A graph that uses dots to represent values for two different numeric variables. | Use to identify relationships or correlations between two variables. | Analysing the relationship between height and weight. |
Histogram | A type of bar chart that represents the distribution of numerical data. | Use to visualise the frequency distribution of a dataset. | Showing the distribution of ages in a survey. |
Pie Chart | A circular chart divided into sectors, each representing a proportion of the whole. | Use to show the composition of a whole, illustrating proportions or percentages. | Displaying market share of different companies in an industry. |
Conclusion
Matplotlib is a versatile library that allows you to create a wide variety of visualizations. By mastering its key functions, you can effectively communicate your data insights and make better decisions. Visualization is a crucial part of data analysis, and Matplotlib makes it accessible and easy to implement.
|
Ready to Visualize Your Data?
Subscribe to our newsletter now and get a free Python cheat sheet! Dive deeper into Python programming with more exciting projects and tutorials designed just for beginners.
Keep exploring, keep coding, and enjoy your journey into data visualization with Matplotlib!