Top Related Projects
The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
Interactive Data Visualization in the browser, from Python
Statistical data visualization in Python
With Holoviews, your data visualizes itself.
Declarative visualization library for Python
Quick Overview
Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It provides a MATLAB-like interface for generating a wide variety of plots, charts, and graphs. Matplotlib is widely used in scientific computing, data analysis, and machine learning for visualizing data and results.
Pros
- Versatile and powerful, capable of creating a wide range of high-quality plots
- Extensive documentation and large community support
- Integrates well with other scientific Python libraries like NumPy and Pandas
- Highly customizable, allowing fine-grained control over plot elements
Cons
- Steep learning curve for beginners due to its extensive functionality
- Default plot styles can look outdated compared to more modern visualization libraries
- Can be slower for large datasets compared to some alternatives
- API can be inconsistent in some areas, leading to confusion
Code Examples
- Creating a simple line plot:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title('Sine Wave')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.show()
- Generating a scatter plot with custom colors and sizes:
import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50)
sizes = 1000 * np.random.rand(50)
plt.scatter(x, y, c=colors, s=sizes, alpha=0.5)
plt.colorbar()
plt.show()
- Creating a bar chart with error bars:
import matplotlib.pyplot as plt
import numpy as np
categories = ['A', 'B', 'C', 'D']
values = [3, 7, 2, 5]
errors = [0.5, 1, 0.3, 0.8]
plt.bar(categories, values, yerr=errors, capsize=5)
plt.title('Bar Chart with Error Bars')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.show()
Getting Started
To get started with Matplotlib, first install it using pip:
pip install matplotlib
Then, import the library and create a simple plot:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.exp(-x/10) * np.cos(2*np.pi*x)
plt.figure(figsize=(8, 6))
plt.plot(x, y)
plt.title('Damped Oscillation')
plt.xlabel('x')
plt.ylabel('y')
plt.grid(True)
plt.show()
This example creates a damped oscillation plot with a title, labels, and grid. Experiment with different plot types and customizations to explore Matplotlib's capabilities.
Competitor Comparisons
The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
Pros of Plotly
- Interactive and dynamic visualizations out-of-the-box
- Easier to create complex, publication-quality plots
- Built-in support for web-based plotting and dashboards
Cons of Plotly
- Steeper learning curve for beginners
- Slower rendering for large datasets
- Requires internet connection for some features
Code Comparison
Matplotlib:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 2, 3]
plt.plot(x, y)
plt.show()
Plotly:
import plotly.graph_objects as go
x = [1, 2, 3, 4]
y = [1, 4, 2, 3]
fig = go.Figure(data=go.Scatter(x=x, y=y))
fig.show()
Both libraries offer powerful plotting capabilities, but Plotly excels in interactive and web-based visualizations, while Matplotlib provides more fine-grained control over plot elements. Matplotlib is often preferred for static plots and scientific publications, while Plotly is popular for creating interactive dashboards and web applications. The choice between the two depends on the specific requirements of your project and the level of interactivity needed in your visualizations.
Interactive Data Visualization in the browser, from Python
Pros of Bokeh
- Interactive and web-friendly visualizations out of the box
- Better suited for large datasets and streaming data
- More modern, customizable UI elements and widgets
Cons of Bokeh
- Steeper learning curve for beginners
- Less extensive documentation and community support
- Fewer built-in statistical and scientific plotting functions
Code Comparison
Matplotlib:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.show()
Bokeh:
from bokeh.plotting import figure, show
p = figure()
p.line([1, 2, 3, 4, 5], [2, 4, 6, 8, 10])
show(p)
Both libraries offer ways to create basic plots, but Bokeh's syntax is more object-oriented and focused on creating interactive web-based visualizations. Matplotlib provides a more familiar interface for those coming from MATLAB or similar environments.
Matplotlib is generally better for static, publication-quality plots and has a wider range of plot types out of the box. Bokeh excels in creating interactive, web-ready visualizations and is more suitable for dashboard-like applications.
Choose Matplotlib for traditional scientific plotting and Bokeh for modern, interactive web-based visualizations.
Statistical data visualization in Python
Pros of Seaborn
- Higher-level interface for creating statistical graphics
- Built-in themes for more aesthetically pleasing default plots
- Specialized plot types for visualizing statistical relationships
Cons of Seaborn
- Less flexibility for highly customized plots
- Smaller community and fewer resources compared to Matplotlib
- Dependent on Matplotlib as its backend
Code Comparison
Matplotlib:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x))
plt.show()
Seaborn:
import seaborn as sns
import numpy as np
x = np.linspace(0, 10, 100)
sns.lineplot(x=x, y=np.sin(x))
sns.despine()
Seaborn provides a more concise and visually appealing default plot, while Matplotlib offers more granular control over plot elements. Seaborn's syntax is often more intuitive for statistical visualizations, but Matplotlib's extensive documentation and larger community can be advantageous for complex or unique plotting needs.
With Holoviews, your data visualizes itself.
Pros of HoloViews
- Higher-level abstraction for easier creation of complex visualizations
- Seamless integration with Pandas DataFrames and xarray datasets
- Powerful composability for combining multiple plots
Cons of HoloViews
- Steeper learning curve for users familiar with Matplotlib
- Less fine-grained control over plot elements
- Smaller community and ecosystem compared to Matplotlib
Code Comparison
Matplotlib:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()
HoloViews:
import holoviews as hv
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
curve = hv.Curve((x, y))
hv.render(curve)
Both libraries can create similar plots, but HoloViews offers a more declarative approach. Matplotlib provides lower-level control, while HoloViews focuses on data-centric visualization with less boilerplate code. HoloViews excels at creating interactive and composable plots, whereas Matplotlib is more suitable for static, highly customized visualizations.
Declarative visualization library for Python
Pros of Altair
- Declarative approach, making it easier to create complex visualizations with less code
- Built on Vega and Vega-Lite, providing a powerful grammar of graphics
- Seamless integration with Jupyter notebooks and web-based environments
Cons of Altair
- Less fine-grained control over plot elements compared to Matplotlib
- Smaller community and ecosystem than Matplotlib
- Limited support for 3D plotting and some specialized chart types
Code Comparison
Altair:
import altair as alt
import pandas as pd
data = pd.DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})
alt.Chart(data).mark_line().encode(x='x', y='y')
Matplotlib:
import matplotlib.pyplot as plt
import pandas as pd
data = pd.DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})
plt.plot(data['x'], data['y'])
plt.show()
Both libraries offer powerful visualization capabilities, but Altair's declarative approach can lead to more concise and readable code for complex visualizations. Matplotlib, however, provides more flexibility and control over individual plot elements, making it better suited for highly customized graphics.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.
Check out our home page for more information.
Matplotlib produces publication-quality figures in a variety of hardcopy formats and interactive environments across platforms. Matplotlib can be used in Python scripts, Python/IPython shells, web application servers, and various graphical user interface toolkits.
Install
See the install
documentation,
which is generated from /doc/install/index.rst
Contribute
You've discovered a bug or something else you want to change â excellent!
You've worked out a way to fix it â even better!
You want to tell us about it â best of all!
Start at the contributing guide!
Contact
Discourse is the discussion forum for general questions and discussions and our recommended starting point.
Our active mailing lists (which are mirrored on Discourse) are:
- Users mailing list: matplotlib-users@python.org
- Announcement mailing list: matplotlib-announce@python.org
- Development mailing list: matplotlib-devel@python.org
Gitter is for coordinating development and asking questions directly related to contributing to matplotlib.
Citing Matplotlib
If Matplotlib contributes to a project that leads to publication, please acknowledge this by citing Matplotlib.
A ready-made citation entry is available.
Top Related Projects
The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
Interactive Data Visualization in the browser, from Python
Statistical data visualization in Python
With Holoviews, your data visualizes itself.
Declarative visualization library for Python
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot