This page includes an interactive code editor. Try modifying and running the examples!
Pandas Data Visualization
Data visualization is essential for understanding patterns, relationships, and trends in your data. Pandas integrates seamlessly with Matplotlib and Seaborn to create powerful visualizations with minimal code.
1. Basic Plotting with Pandas
Pandas DataFrame and Series objects have built-in plotting methods that leverage Matplotlib.
- Line plots:
df.plot()ordf.plot.line() - Bar plots:
df.plot.bar()ordf.plot.barh() - Histograms:
df.plot.hist() - Box plots:
df.plot.box() - Area plots:
df.plot.area()
| Plot Type | Best For | Pandas Method |
|---|---|---|
| Line Plot | Time series, trends over time | .plot() or .plot.line() |
| Bar Plot | Categorical comparisons | .plot.bar() |
| Histogram | Distribution of numerical data | .plot.hist() |
| Box Plot | Statistical distribution overview | .plot.box() |
| Scatter Plot | Relationship between two variables | .plot.scatter() |
2. Advanced Pandas Plotting
Pandas offers specialized plotting functions for complex visualizations.
- Scatter matrix:
pd.plotting.scatter_matrix() - Autocorrelation:
pd.plotting.autocorrelation_plot() - Bootstrap plot:
pd.plotting.bootstrap_plot() - Lag plot:
pd.plotting.lag_plot()
3. Seaborn Integration
Seaborn provides high-level interface for statistical visualizations.
- Distribution plots:
sns.histplot(),sns.kdeplot() - Categorical plots:
sns.boxplot(),sns.violinplot() - Relational plots:
sns.scatterplot(),sns.lineplot() - Matrix plots:
sns.heatmap(),sns.clustermap()
4. Plot Customization
Customize plots for better presentation and clarity.
- Styling:
plt.style.use() - Colors: Color maps, custom palettes
- Annotations: Text, arrows, labels
- Layout: Subplots, figure size, spacing
Choosing the Right Visualization
📈 Trends Over Time
- Line plots
- Area plots
- Stacked area
📊 Comparisons
- Bar charts
- Grouped bars
- Radar charts
🔍 Distributions
- Histograms
- Box plots
- Violin plots
🔗 Relationships
- Scatter plots
- Bubble charts
- Correlation heatmaps
🍕 Proportions
- Pie charts
- Donut charts
- Stacked bar charts
🌐 Composition
- Stacked area
- Tree maps
- Sunburst charts
Example: Comprehensive Data Visualization
Data Visualization Examples
Essential Plotting Methods
.plot()- Basic plotting.plot.kind()- Specific plot typesplt.subplots()- Multiple plotssns.FacetGrid()- Multi-plot gridsplt.savefig()- Save plots
Customization Options
- Colors and styles
- Labels and titles
- Legends and annotations
- Figure size and DPI
- Grids and axes
Best Practices:
- Choose the right chart type for your data
- Keep visualizations simple and clear
- Use appropriate colors and labels
- Consider your audience when designing plots
- Always include titles and axis labels
Pro Tip: Use
plt.tight_layout() to automatically adjust subplot parameters and avoid overlapping elements.