Python Data Visualization with Matplotlib: Steps, Functions, and Practical Examples
This article provides a comprehensive guide to creating visualizations in Python using Matplotlib and related libraries, covering the three‑step workflow of problem definition, data transformation, and parameter tuning, along with detailed code examples for figures, subplots, legends, annotations, saving files, and Pandas‑based plotting.
The article introduces Python's core visualization libraries— matplotlib as the foundational tool, seaborn for higher‑level statistical graphics, and mentions additional options such as Bokeh and Mapbox .
Step 1: Define the problem and choose the chart type – select an appropriate visual element (scatter, line, bar, heatmap) based on the data relationship.
Step 2: Transform the data and apply functions – perform merging, reshaping, deduplication, mapping, filling, and renaming before feeding the data to plotting functions.
Step 3: Set parameters for a clear presentation – adjust colors, line styles, markers, titles, axis labels, ticks, and legends to make the plot intuitive.
Matplotlib draws all graphics on a Figure canvas; individual axes are created with fig = plt.figure(); ax = fig.add_subplot(1,1,1) or more conveniently with fig, axes = plt.subplots(2,2, sharex=True, sharey=True) . Figure size can be set via the figsize argument.
Common customizations include: Color and line style: plt.plot(np.random.randn(30), color='g', linestyle='--', marker='o') Axis limits and ticks: plt.xlim([0,15]) , ax.set_xticks([0,250,500,750,1000]) , ax.set_xticklabels(['one','two','three','four','five']) Titles and labels: ax.set_title('My first Plot') , ax.set_xlabel('Stage') Legends: add label='one' to plot calls and invoke ax.legend(loc='best') Annotations: plt.text(600,10,'test', family='monospace', fontsize=10) Saving figures: plt.savefig('./plot.jpg') with optional parameters dpi , bbox_inches='tight'
Using Pandas, the same visualizations can be generated with far fewer lines: Series plot: s = pd.Series(np.random.randn(10).cumsum(), index=np.arange(0,100,10)) then s.plot() DataFrame plot: df = pd.DataFrame(np.random.randn(10,4).cumsum(0), columns=['A','B','C','D']) then df.plot() Bar charts: data.plot(kind='bar', ax=axes[0], rot=0, alpha=0.3) and data.plot(kind='barh', ax=axes[1], grid=True)
The article concludes that mastering these three steps—thinking, choosing, applying—along with regular practice will make Python data visualization proficient and adaptable to various analytical tasks.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.