
Introduction
Data visualization is the practice of converting data, which can often be complex and difficult to understand in raw form, into visual representations like charts, graphs, and maps. This approach makes it easier to see patterns and trends in large datasets.
Here's why data visualization is so important in data analysis:
- Enhanced Understanding: Visuals can communicate complex data in a more intuitive and digestible way than tables of numbers.
- Quick Decision-Making: By presenting data visually, decision-makers can grasp difficult concepts or identify new patterns quickly, leading to faster and more effective decision-making.
- Better Communication: Visuals are a universal language. They can be understood by a diverse audience, making it easier to communicate findings to people who aren’t data experts.
- Revealing Connections: Visualizations can highlight relationships and correlations between variables that might not be apparent from raw data alone.
Data visualization transforms raw data into a visual context, making the data more accessible and understandable, which is crucial in the data-driven world of today.
Python is a popular choice for turning data into easy-to-understand visual forms like charts and graphs. It's great for beginners because it's easy to learn and use. Plus, Python has tools like Jupyter Notebooks which make it even more user-friendly, especially for seeing your data in real-time. It offers a variety of special packages including, such as Matplotlib, Seaborn, Plotly, and Pandas Visualization, which are perfect for creating everything from simple charts to complex, interactive graphics.
What's more, Python works really well with other tools that help organize and understand data, making it a go-to for the entire process of analyzing data and showing it visually.
Let’s dive into the world of Python data visualization with Matplotlib:
MATPLOTLIB
Developed as an alternative to MATLAB's plotting capabilities, Matplotlib aimed to offer an open-source and more accessible solution for Python users.
Its role in Python is significant due to its comprehensive range of plotting functions and its flexibility to create both simple and complex visualizations.
Matplotlib's ability to integrate with other libraries, like NumPy for numerical data and Pandas for data manipulation, further cements its place as a versatile and essential tool for data analysts and scientists.
Let's create some basic visualizations with Matplotlib
Our dataset is a detailed compilation of 2023's top hits on Spotify, it sheds light on song attributes, cross-platform presence, and more.
Line Plot
Line Plots are ideal for showing trends over time. They connect data points with a straight line, they are typically used in time series data.
We will create a line plot highlighting the number of songs streamed in each year to find out if we can identify any trends
# Plotting the data
grouped_df.plot(kind='line', x='released_year', y='streams')
plt.xlabel('Year')
plt.ylabel('Number of Streams')
plt.title('Song Streams Over The Years');- grouped_df: This is the DataFrame object. It holds the data you're plotting.
- .plot(): This is a method provided by matplotlib and pandas to create a variety of plot types.
- kind='line': This argument specifies the type of plot to create
- x='released_year': This specifies which column from the DataFrame should be used for the x-axis values. Here, it's the 'released_year' column.
- y='streams': This sets the column to be used for y-axis values, which is 'streams' in your dataset. This column likely contains numerical data representing the number of streams.
- plt.xlabel('Year'): This line is using Matplotlib’s xlabel function to set the label of the x-axis. The label here is 'Year', which gives context to what the x-axis represents in the plot.
- plt.ylabel('Number of Streams'): Similar to the previous line, ylabel is used to set the label of the y-axis. 'Number of Streams' clearly indicates that the y-axis represents the count or quantity of streams.
- plt.title('Song Streams Over The Years'): This line sets the title of the plot using the title function. 'Song Streams Over The Years' gives a general idea about what the plot is depicting.
# Setting up the formatter for the y-axis
y_formatter = ScalarFormatter(useOffset=False, useMathText=False)
y_formatter.set_scientific(False)
plt.gca().yaxis.set_major_formatter(y_formatter)- y_formatter = ScalarFormatter(useOffset=False, useMathText=False): This line creates a tool (y_formatter) to control how numbers look on the y-axis.
It's set up to show numbers as they are, without adding or subtracting any value (that's what useOffset=False means).
Also, it avoids using complicated math symbols (useMathText=False).
- y_formatter.set_scientific(False): This part tells the tool not to use scientific notation. In simple terms, it avoids showing large numbers in a short form like 1e6 (which is a way to write 1,000,000).
- plt.gca().yaxis.set_major_formatter(y_formatter): This applies the settings from the tool (y_formatter) to the y-axis of the graph.
It ensures that all the main numbers on the y-axis follow the rules we set: no offsets, no scientific notation, just plain numbers as they are.
# Showing the plot
plt.show()- o
- plt.show(): This line is used to display the plot that you have created
Bar Chart
Bar Charts are great for comparing quantities across different categories. Using vertical or horizontal bars to show comparisons among discrete categories.
We will create a bar chart highlighting the top 10 streamed songs
# Plotting the data
plt.bar(top_10_streamed_songs['track_name'], top_10_streamed_songs['streams'])
plt.xlabel('track_name')
plt.ylabel('Number of Streams')
plt.title('Top 10 Streamed Songs');
# Setting up the formatter for the y-axis
y_formatter = ScalarFormatter(useOffset=False, useMathText=False)
y_formatter.set_scientific(False)
plt.gca().yaxis.set_major_formatter(y_formatter)
plt.xticks(rotation=90, ha='center')
# Showing the plot
plt.show();- plt.bar(top_10_streamed_songs['track_name'], top_10_streamed_songs['streams']): This line creates a bar chart. The horizontal axis (x-axis) shows the track names from the data and the vertical axis (y-axis) shows the number of streams for each track.
- plt.xlabel('track_name') and plt.ylabel('Number of Streams'): These lines add labels to the x-axis and y-axis of the chart, so you know one axis represents the track names and the other represents the number of streams.
- plt.title('Top 10 Streamed Songs'): This adds a title to the chart, telling viewers that it's about the "Top 10 Streamed Songs".
- plt.xticks(rotation=90, ha='center'): This rotates the labels on the x-axis (track names) by 90 degrees and aligns them to the center. It’s done to make the labels more readable.
- plt.show(): Finally, this line displays the bar chart created. It opens up a window showing the chart with all the settings and data added.
Histograms are best for showing the distribution of a dataset. It groups data into bins and is useful for understanding the distribution or frequency of data.
plt.hist(data["in_spotify_charts"])
plt.xlabel('In_spotify_charts')
plt.ylabel('Frequency')
plt.title('Distribution of the number of Spotify charts a song is in')
plt.show();- plt.hist(data["in_spotify_charts"]): This line makes a histogram for the values in the in_spotify_charts column of the data dataset. The histogram will show how many songs are in different numbers of Spotify charts.
- plt.xlabel('In_spotify_charts'): Sets the label for the x-axis as 'In_spotify_charts', indicating this axis represents the number of Spotify charts a song is in.
- plt.ylabel('Frequency'): Sets the label for the y-axis as 'Frequency', showing how often each range of chart counts occurs in the data.
- plt.title('Distribution of the number of Spotify charts a song is in'): Adds a title to the chart, describing what it's about - the distribution of songs across different numbers of Spotify charts.
- plt.show(): Finally, this line displays the histogram created.
Box Plot
Box Plots are ideal for statistical analysis, such as detecting outliers and understanding the spread of data. It shows the distribution of data based on a five-number summary: minimum, first quartile, median, third quartile, and maximum.
Let’s create a boxplot to analyze the number of streams a song has:
# Creating histogram
plt.boxplot(data["streams"], vert = False)
plt.xlabel('Number of Streams')
plt.title('Songs Streamed')
plt.xticks(rotation=90, ha='center')
x_formatter = ScalarFormatter(useOffset=False, useMathText=False)
x_formatter.set_scientific(False)
plt.gca().xaxis.set_major_formatter(x_formatter)
plt.show();
- plt.boxplot(data["streams"], vert = False): This line generates a boxplot for the 'streams' column in the 'data' dataset. The vert = False part makes the boxplot horizontal.
- plt.xlabel('Number of Streams'): Adds a label to the x-axis, indicating that it represents the number of streams.
- plt.title('Songs Streamed'): Places a title above the chart, informing viewers that it's about songs streamed.
- plt.xticks(rotation=90, ha='center'): Rotates the labels on the x-axis by 90 degrees and aligns them to the center, which can help make them more readable.
- x_formatter = ScalarFormatter(useOffset=False, useMathText=False)
x_formatter.set_scientific(False)
plt.gca().xaxis.set_major_formatter(x_formatter)
These lines set up a formatter for the x-axis to display the numbers in a simple, non-scientific format (plain numbers without complex notation).
Scatter Plot
Scatter plots are a type of graph ideal for visualizing how two different continuous (numerical) variables relate to each other. Here's a brief explanation:
- Two Variables: A scatter plot displays points that each represent two different pieces of data. One variable is plotted along the horizontal (x-axis) and the other along the vertical (y-axis).
- Relationships and Patterns: By looking at where the points fall on the plot, you can see if there is a relationship or pattern between the two variables. For example, if the points trend upwards from left to right, this suggests that as one variable increases, so does the other.
- Individual Data Points: Each point on a scatter plot represents an individual data entry. For instance, in a plot showing height and weight, each point would represent the height and weight of one person.
In essence, scatter plots help in visually exploring and displaying the relationship between two numerical variables, making it easier to identify trends, correlations, or outliers in the data.
Using a scatter plot, let’s visualize the relationship between the number of spotify playlists a song is in and the total number of streams it has
plt.scatter(data['in_spotify_playlists'], data['streams'], color = "b")
plt.xlabel('In spotify playlists')
plt.ylabel('Streams')
plt.title("In spotify playlists vs Streams")
y_formatter = ScalarFormatter(useOffset=False, useMathText=False)
y_formatter.set_scientific(False)
plt.gca().yaxis.set_major_formatter(y_formatter)
plt.show();- plt.scatter(data['in_spotify_playlists'], data['streams'], color = "b"): This line plots each song as a point, with its position determined by its number of Spotify playlists (on the x-axis) and its streams (on the y-axis). The color = "b" part sets the color of the points to blue.
- plt.xlabel('In spotify playlists') and plt.ylabel('Streams'): These lines add labels to the x-axis and y-axis, clearly indicating what each axis represents.
- plt.title("In spotify playlists vs Streams"): Adds a title to the plot, summarizing what the plot is about.
- y_formatter = ScalarFormatter(useOffset=False, useMathText=False)
y_formatter.set_scientific(False)
plt.gca().yaxis.set_major_formatter(y_formatter)
These lines set up a formatter for the y-axis to display the numbers in a non-scientific format, meaning streams are shown as regular numbers without scientific notation.
These are a few visualizations that can be created with matplotlib. Mastering data visualization in Python, especially through libraries like Matplotlib, is an essential skill in today’s data-centric world. Whether you're a beginner or an experienced data analyst, Python offers the tools and simplicity needed to transform complex data into clear, impactful visuals.



