1. Matplotlib
The most basic and widely used Python plotting library
import matplotlib.pyplot as plt
import numpy as np
# Create data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Plot line chart
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'b-', linewidth=2, label='sin(x)')
plt.title('Sine Function')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.legend()
plt.grid(True)
plt.show()
2. Seaborn
High-level statistical data visualization library based on Matplotlib
import seaborn as sns
import pandas as pd
# Load example data
tips = sns.load_dataset('tips')
# Plot scatter plot with regression line
plt.figure(figsize=(10, 6))
sns.scatterplot(data=tips, x='total_bill', y='tip', hue='time')
sns.regplot(data=tips, x='total_bill', y='tip', scatter=False)
plt.title('Relationship between Tips and Total Bill Amount')
plt.show()
# Plot box plot
plt.figure(figsize=(10, 6))
sns.boxplot(data=tips, x='day', y='total_bill', hue='smoker')
plt.title('Daily Bill Amount Distribution')
plt.show()
3. Plotly
Interactive visualization library
import plotly.express as px
# Create interactive scatter plot
df = px.data.iris()
fig = px.scatter(df, x='sepal_width', y='sepal_length', color='species', size='petal_length', hover_data=['petal_width'])
fig.show()
# Create interactive 3D scatter plot
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_length', color='species', size='petal_width')
fig.show()
4. Built-in Pandas Plotting
Quick visualization directly using DataFrame
import pandas as pd
import numpy as np
# Create example data
df = pd.DataFrame({ 'A': np.random.randn(100), 'B': np.random.randn(100), 'C': np.random.choice(['X', 'Y', 'Z'], 100)})
# Use pandas plotting
df['A'].hist(bins=20, alpha=0.7)
plt.title('Distribution of Column A')
plt.show()
df.groupby('C')['B'].mean().plot(kind='bar')
plt.title('Average of B Grouped by C')
plt.show()