0% found this document useful (0 votes)
0 views4 pages

Python Plotting Basics

The document provides a series of examples for creating various types of plots using Matplotlib and Seaborn in Python. It covers simple line plots, bar charts, scatter plots, histograms, boxplots, and heatmaps, along with customization options and saving plots as images. Each example includes code snippets demonstrating how to generate the respective visualizations.

Uploaded by

kate001atieno
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)
0 views4 pages

Python Plotting Basics

The document provides a series of examples for creating various types of plots using Matplotlib and Seaborn in Python. It covers simple line plots, bar charts, scatter plots, histograms, boxplots, and heatmaps, along with customization options and saving plots as images. Each example includes code snippets demonstrating how to generate the respective visualizations.

Uploaded by

kate001atieno
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/ 4

1.

Simple Line Plot using Matplotlib

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]

y = [2, 4, 1, 3]

plt.plot(x, y)

plt.title("Simple Line Plot")

plt.xlabel("X-axis")

plt.ylabel("Y-axis")

plt.grid(True)

plt.show()

2. Plotting a DataFrame using Pandas

import pandas as pd

import matplotlib.pyplot as plt

data = {'Month': ['Jan', 'Feb', 'Mar', 'Apr'], 'Sales': [100, 120, 90, 110]}

df = pd.DataFrame(data)

df.plot(x='Month', y='Sales', kind='line', marker='o', title='Monthly Sales')

plt.ylabel("Sales")

plt.grid(True)

plt.show()

3. Customizing Plot Colors, Line Style, and Markers

plt.plot(x, y, color='green', linestyle='--', marker='s') # dashed green line with square markers
plt.title("Customized Line Plot")

plt.show()

4. Bar Chart in Python using Matplotlib

categories = ['A', 'B', 'C']

values = [5, 7, 3]

plt.bar(categories, values, color='orange')

plt.title("Bar Chart Example")

plt.ylabel("Value")

plt.show()

5. Scatter Plot using Seaborn

import seaborn as sns

tips = sns.load_dataset("tips")

sns.scatterplot(data=tips, x="total_bill", y="tip")

plt.title("Bill vs Tip")

plt.show()

6. Creating Subplots in Matplotlib

fig, axs = plt.subplots(1, 2) # 1 row, 2 columns

axs[0].plot([1, 2, 3], [1, 4, 9])

axs[0].set_title("Plot 1")

axs[1].bar(['A', 'B', 'C'], [5, 7, 6])


axs[1].set_title("Plot 2")

plt.tight_layout()

plt.show()

7. Histogram to Show Data Distribution

import numpy as np

data = np.random.randn(1000)

plt.hist(data, bins=20, color='purple')

plt.title("Histogram")

plt.xlabel("Value")

plt.ylabel("Frequency")

plt.show()

8. Boxplot using Seaborn

sns.boxplot(data=tips, x="day", y="total_bill")

plt.title("Boxplot of Total Bills by Day")

plt.show()

9. Correlation Heatmap using Seaborn

corr = tips.corr(numeric_only=True)

sns.heatmap(corr, annot=True, cmap="coolwarm")

plt.title("Correlation Heatmap")

plt.show()

10. Saving a Plot as an Image


plt.plot([1, 2, 3], [4, 5, 6])

plt.savefig("my_plot.png") # Saves the current plot as a PNG file

You might also like