Python Data Visualization with Matplotlib
Learn data plotting in Python. Create clean scatter plots, adjust labels, legends, and styling natively in the browser.
How it Works
Data visualization is the graphical representation of information and data. By using visual elements like charts, graphs, and maps, data visualization tools provide an accessible way to see trends, outliers, and patterns.
Matplotlib is the core library for plotting in Python. It provides an object-oriented API that lets you customize every single pixel of your figure, from axes colors to font styles.
In this example, we generate synthetic growth curves and plot them on the same figure, showing how to define line thickness, markers, legends, and axis limits for a publication-ready graph.
Source Code
A script using matplotlib to render a comparison chart of mathematical functions.
import matplotlib.pyplot as plt
import numpy as np
# Generate points along the X axis
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
# Create figure
fig, ax = plt.subplots(figsize=(8, 5))
# Plot lines
ax.plot(x, y1, label='Sine Wave', color='#0D9488', linewidth=2.5)
ax.plot(x, y2, label='Cosine Wave', color='#EF4444', linewidth=2, linestyle='--')
# Style axis and frame
ax.set_title('Trigonometric Waveforms', fontsize=14, fontweight='bold', pad=15)
ax.set_xlabel('X Axis (Radians)', fontsize=11)
ax.set_ylabel('Amplitude', fontsize=11)
ax.grid(True, linestyle=':', alpha=0.6)
ax.legend(loc='upper right', frameon=True)
ax.spines[['top', 'right']].set_visible(False)
plt.tight_layout()
plt.show()Rendered visualization in the Graphs tab...Real-world Applications
- Scientific research plotting and telemetry output
- Creating dashboard chart elements for analytical web apps
- Exploratory data analysis during machine learning tasks
Frequently Asked Questions
Can I use seaborn with PyRun?
Yes, seaborn is a wrapper built on top of matplotlib. If imported, PyRun redirects its plots into the same browser Graph viewport.
How do I save a matplotlib plot to a file in Python?
Instead of calling `plt.show()`, you can run `plt.savefig('my_chart.png', dpi=300)` to write the image directly into your file system workspace.