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