0% found this document useful (0 votes)
17 views12 pages

iQRcDEQBTHLdcA6Ncp4A Miuul Data Visualization Cheat Sheet

This document is a cheat sheet for data visualization using Python's Matplotlib and Seaborn libraries. It provides examples and code snippets for creating various types of plots including line plots, scatter plots, bar plots, histograms, box plots, heatmaps, pie charts, and multiple subplots. Additionally, it includes a section on customizing plots for better presentation.

Uploaded by

burak Kaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views12 pages

iQRcDEQBTHLdcA6Ncp4A Miuul Data Visualization Cheat Sheet

This document is a cheat sheet for data visualization using Python's Matplotlib and Seaborn libraries. It provides examples and code snippets for creating various types of plots including line plots, scatter plots, bar plots, histograms, box plots, heatmaps, pie charts, and multiple subplots. Additionally, it includes a section on customizing plots for better presentation.

Uploaded by

burak Kaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Data Visualization

Cheat Sheet
Importing Libraries

import matplotlib.pyplot as plt


import seaborn as sns
01
Basic Line Plot
plt.plot(x, y, linestyle = '-', color = 'b')
plt.xlabel('X-axis label')
plt.ylabel('Y-axis label')
plt.title('Title')
plt.show()

Example

# Sample dataset
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
revenue = [10000, 15000, 12000, 18000, 20000]

# Create the line plot


plt.plot(months, revenue, marker='o', linestyle='-', color='b')
plt.xlabel('Months')
plt.ylabel('Revenue ($)')
plt.title('Monthly Revenue')
plt.grid(True)
plt.show()
Scatter Plot 02
plt.scatter(x, y, c='color')
plt.xlabel('X-axis label')
plt.ylabel('Y-axis label')
plt.title('Title')
plt.show()

Example

# Load the tips dataset


df = sns.load_dataset("tips")

# Extract the total bill and tip amounts


total_bill = df["total_bill"].values
tip_amount = df["tip"].values

# Create a scatter plot


plt.scatter(total_bill, tip_amount, c='green')
plt.xlabel('Total Bill')
plt.ylabel('Tip Amount')
plt.title('Scatter Plot: Tips Dataset')
plt.show()
Bar Plot 03
plt.bar(x, y, color = 'b', width = 0.5, height = 0.3)
plt.xlabel('X-axis label')
plt.ylabel('Y-axis label')
plt.title('Title')
plt.show()

# Horizontal bar
plt.barh(x, y)
plt.show()

Example

# Load the 'tip' dataset from seaborn


tips = sns.load_dataset('tips')

# Calculate the average tip amount for each day of the week
avg_tip_by_day = tips.groupby('day')['tip'].mean()

# Create the bar plot


sns.barplot(x=avg_tip_by_day.index, y=avg_tip_by_day.values)
plt.xlabel('Day of the Week')
plt.ylabel('Average Tip Amount')
plt.title('Average Tip Amount by Day of the Week')
plt.show()
Histogram 04
plt.hist(data, bins=10)
plt.xlabel('X-axis label')
plt.ylabel('Y-axis label')
plt.title('Title')
plt.show()

Example
# Load the 'tip' dataset from seaborn
tips = sns.load_dataset('tips')

# Create the histogram


sns.histplot(data=tips, x='tip', bins=10, color='green')
plt.xlabel('Tip Amount')
plt.ylabel('Count')
plt.title('Distribution of Tip Amounts')
plt.show()
05 Box Plot
sns.boxplot(data, x, y, color)
plt.xlabel('X-axis label')
plt.ylabel('Y-axis label')
plt.title('Title')
plt.show()

Example

# Load the 'tip' dataset from seaborn


tips = sns.load_dataset('tips')

# Create the box plot with a different color


sns.boxplot(data=tips, x='day', y='total_bill', color='green')
plt.xlabel('Day of the Week')
plt.ylabel('Total Bill Amount')
plt.title('Distribution of Total Bills by Day of the Week')
plt.show()
06 Heatmap
sns.heatmap(data, annot=True)
plt.xlabel('X-axis label')
plt.ylabel('Y-axis label')
plt.title('Title')
plt.show()

Example

# Load the 'tip' dataset from seaborn


tips = sns.load_dataset('tips')

# Calculate the correlation matrix


correlation_matrix = tips.corr()

# Create the heatmap


sns.heatmap(correlation_matrix, annot=True, cmap='YlGn')
plt.title('Correlation Heatmap')
plt.show()
07 Pie Chart

plt.pie(y, labels=labels, colors=[colors])


plt.title('Title')
plt.show()

Example

# Load the 'tip' dataset from seaborn


tips = sns.load_dataset('tips')

# Calculate the count of meals for each time of the day


meal_counts = tips['time'].value_counts()

# Specify custom labels and colors


labels = meal_counts.index
colors = ['darkgreen', 'aquamarine']

# Create the pie chart


plt.pie(meal_counts, labels=labels, colors=colors)
plt.title('Distribution of Meals by Time of the Day')
plt.show()
08 Multiple Subplots
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x1, y1)
axs[0, 1].scatter(x2, y2)
axs[1, 0].bar(x3, height3)
axs[1, 1].hist(data4, bins=10)
plt.show()

# Load the 'tips' dataset from seaborn


tips = sns.load_dataset('tips')

# Prepare the data for plotting


x1 = tips['total_bill']
y1 = tips['tip']
x2 = tips['size']
y2 = tips['tip']
x3 = tips['day']
height3 = tips['total_bill']
data4 = tips['total_bill']

# Create the subplots


fig, axs = plt.subplots(2, 2)

# Plot 1: Line plot


axs[0, 0].plot(x1, y1)
axs[0, 0].set_xlabel('Total Bill')
axs[0, 0].set_ylabel('Tip')
axs[0, 0].set_title('Tip Amount vs Total Bill')
08.01 Multiple Subplots

# Plot 2: Scatter plot


axs[0, 1].scatter(x2, y2)
axs[0, 1].set_xlabel('Party Size')
axs[0, 1].set_ylabel('Tip')
axs[0, 1].set_title('Tip Amount vs Party Size')

# Plot 3: Bar plot


axs[1, 0].bar(x3, height3)
axs[1, 0].set_xlabel('Day')
axs[1, 0].set_ylabel('Total Bill')
axs[1, 0].set_title('Total Bill by Day')

# Plot 4: Histogram
axs[1, 1].hist(data4, bins=10)
axs[1, 1].set_xlabel('Total Bill')
axs[1, 1].set_ylabel('Frequency')
axs[1, 1].set_title('Distribution of Total Bills')

# Adjust the layout and spacing


fig.tight_layout()

# Display the plot


plt.show()
09 Customizing Plots
plt.figure(figsize=(8, 6))
plt.plot(x, y, color='red', linestyle='--',
linewidth=2, marker='o', markersize=6)
plt.xlabel('X-axis label', fontsize=12)
plt.ylabel('Y-axis label', fontsize=12)
plt.title('Title', fontsize=14)
plt.legend(['Legend'])
plt.grid(True)
plt.show()

Example

# Load the 'tip' dataset from seaborn


tips = sns.load_dataset('tips')

# Prepare the data for plotting


x = tips['total_bill']
y = tips['tip']

# Create the plot


plt.figure(figsize=(8, 6))
plt.plot(x, y, color='red', linestyle='--', linewidth=2,
marker='o', markersize=6)
plt.xlabel('Total Bill', fontsize=12)
plt.ylabel('Tip', fontsize=12)
plt.title('Tip Amount vs Total Bill', fontsize=14)
plt.legend(['Tips'])
plt.grid(True)
plt.show()

You might also like