0% found this document useful (0 votes)
68 views35 pages

Practical Guide To Matplotlib For Data Science - 1689973407325

Practical Guide to Matplotlib for Data Science - 1689973407325

Uploaded by

Dridi Amani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
68 views35 pages

Practical Guide To Matplotlib For Data Science - 1689973407325

Practical Guide to Matplotlib for Data Science - 1689973407325

Uploaded by

Dridi Amani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 35
micelles Ee ules) to Matploilio for Data Science ‘S) A STEP-BY-STEP GUIDE | Table of Contents Introduction to Matplotlib © 1.1 What is Matplotlib? © 1.2 Why Use Matplotlib in Data Science? Installation and Setup © 2.1 Installing Matplotlib © 2.2 Importing Matplotlib Basic Plots © 3.1 Line Plots © 3.2 Scatter Plots © 3.3 Bar Plots © 3.4 Histograms Customizing Plots © 4.1 Figure Size and Resolution © 4.2 Labels and Titles © 4.3 Legends © 4.4 Colors, Styles, and Markers © 4.5 Axis Limits and Ticks Multiple Subplots © 5.1 Creating Subplots © 5.2 Customizing Subplots © 5.3 Sharing Axis Labels Advanced Plots © 6.1 Pie Charts © 6.2 Box Plots © 6.3 Heatmaps © 6.4 3D Plots Data Visualization Tips © 7.1 Choosing the Right Plot © 7.2 Using Colors Effectively © 7.3 Handling Missing Data © 7.4 Adding Annotations and Text Saving and Exporting Plots © 8.1 Saving Plots to File © 8.2 Exporting Plots to Different Formats Real-World Examples © 9.1 Plotting Time Series Data © 9.2 Visualizing Categorical Data © 9.3 Geospatial Data Visualization Conclusion @RAMCHANDRAPADWAL ~ Introduction to Matplottilo 1.1 WHAT IS MATPLOTLIB? Matplotlib is a popular data visualization library in Python that provides a wide range of plotting functions and tools. It allows data scientists to create high-quality charts, graphs, and visualizations with ease. 1.2 WHY USE MATPLOTLIB IN DATA SCIENCE? Matplotlib is widely used in data science due to its flexibility, extensive functionality, and compatibility with other libraries like NumPy and Pandas. It offers a vast array of plot types, customization options, and excellent documentation, making it an essential tool for data exploration and presentation. @RAMCHANDRAPADWAL, fe aaaa\ 4 Installation and S10 /@ 2.1 Installing Matplotlib To install Matplotlib, you can use pip, the Python package manager. Open a terminal and execute the following command: pip install matplotlib 2.2 Importing Matplotlib Before using Matplotlib, import it into your Python script or notebook using the following statement: Pesta Canoe @RAMCHANDRAPADWAL, fo aaa aN ec} (oleae Sys Son @lks ‘S) Tee mae Lene 3.1 Line Plots | Line plots are useful for visualizing the relationship between | two variables over a continuous interval. To create a line plot, use the plot() function in Matplotlib. EXAMPLE: Chats Cob 7 SC Lae ee sed plot(x, y) aera EUS Rotola show() OUTPUT: Line Plot Yaxis @RAMCHANDRAPADWAL 3.2 Scatter Plots Scatter plots are ideal for displaying the distribution and relationship between two numerical variables. Matplotlib provides the scatter() function for creating scatter plots. EXAMPLE: POSS ISe LE aa bd catter(x Pia ssl Pcaagersn plt.title( plt.show() OUTPUT: Scatter Plot ¥axis lo 15 20 25 30 35 40 45 50 Xaxis @RAMCHANDRAPADWAL 3.3 Bar Plots Bar plots are effective for comparing categorical data or showing the distribution of a single variable. Matplotlib offers the bar() and barh() functions for creating vertical and horizontal bar plots, respectively. EXAMPLE: Teese 7 lee ed categories values = [10, plt.bar(categories, values) nee sean aan EL nt seen an plt.show() OUTPUT: Bar Plot Values oo A 8 c D Categories @RAMCHANDRAPADWAL, 3.4 Histograms Histograms are useful for visualizing the distribution of a continuous variable. Matplotlib provides the hist() function to create histograms. EXAMPLE: matplotlib.pyplot as plt eee) Ce ecu ecg Prarie) earn » BBtIasKt Recatye -show() OUTPUT: Histogram 80 2 g Frequency =3 2 =1 ° B 3 Values @RAMCHANDRAPADWAL, CHAPTER N.4 OUSrelanvaiale} Plots 4.1 Figure Size and Resolution Matplotlib allows you to customize the size and resolution of your plots using the figure() function EXAMPLE: matplotlib.pyplot [saa ) Uses cect St CRO Co plt.show() ouTPUT: @RAMCHANDRAPADWAL, 4.2 Labels and Titles You can add labels to the x-axis, y-axis, and title of your plot using the xlabel(), ylabel(), and title() functions. EXAMPLE: DOSS ISe LES aa hd stare en) [tae plt. ylabel( istaesot| how’) OUTPUT: Line Plot ¥axis @RAMCHANDRAPADWAL 4.3 Legends To add a legend to your plot, use the legend() function. You can specify the position of the legend and add labels for each data series. EXAMPLE: matplotlib.pyplot as plt Cota trae sens aE Pye eTSee) Br PotaeCUCierd — series 1 — series 2 @RAMCHANDRAPADWAL 4.4 Colors, Styles, and Markers Matplotlib allows you to customize the colors, line styles, and markers of your plots. You can use a wide range of predefined colors and markers or define your own. EXAMPLE: Ce RIEU Cae Sed Faces ACU p teen aoe @RAMCHANDRAPADWAL, 4.5 Axis Limits and Ticks You can set the limits and ticks for the x-axis and y-axis using the xtim(), ylim(), and xticks(), yticks() functions, respectively EXAMPLE: iT SSE CN SC aeee ed [stat eae Pstae sec On OUTPUT: 207 @RAMCHANDRAPADWAL Sen Mirels Subplots 5.1 Creating Subplots Matplotlib allows you to create multiple subplots within a single figure using the subplots() function. EXAMPLE: PL Cot nZone ad Par axes. flatten() (data, bins ) OUTPUT: 10 10 ° 8 8 ° 6 6 e 4 4 ° 2 240 1 2 3 4 5 1 2 3 4 5 20 80 1s 60 10 40 5 20 0 ° A B ig D -2 0 2 @RAMCHANDRAPADWAL, 5.2 Customizing Subplots You can customize each subplot individually by accessing them through the axes object. EXAMPLE: Ce CSE UN Coed fig, axes = plt.subplots(nrows=2, ncols=2) , ax4 = axes. flatten() cmBeTsnd seem) Eee Cansei Cramatong 10 @RAMCHANDRAPADWAL, 5.3 Sharing Axis Labels When creating subplots, you can share the x-axis or y-axis labels across multiple subplots. EXAMPLE: Ceo Se LMT aCe yc Mersretir Peon eae vc vce sto ule) Pye Te) OUTPUT: 10 08 06 04 02 0.0 10 08 06 04 02 0.0 00 02 04 06 08 10 00 02 04 06 08 10 @RAMCHANDRAPADWAL CoN aas aN Ro} Advanced Plots _ 6.1 Pie Charts Pie charts are ideal for displaying proportions and percentages of different categories. Matplotlib provides the pie() function for creating pie charts. EXAMPLE: Cease, aes 1 1 SEMEL ae OuTPUT: Pie Chart 6.2 Box Plots Box plots are useful for visualizing the distribution of numerical data through quartiles. Matplotlib offers the boxplot() function for creating box plots. EXAMPLE: DESC LSE Ue 7) Cea Ses Chem recur CMake plt.boxplot (data) SaEUSNG BES OUTPUT: Box Plot | {0 Values 1 2 3 Groups 6.3 Heatmaps Heatmaps are excellent for visualizing matrices or correlation matrices. Matplotlib provides the imshow() function for creating heatmaps. EXAMPLE: Ce SCS ECM eg oe Ce ecu cUC La TICE CL mototlg show() OUTPUT: Ded 10) SCLC Heatmap 6.4 3D Plots Matplotlib also supports 3D plotting for visualizing complex data. You can create 3D plots using the plot_surface() function. EXAMPLE: ee CStCeey ca ao) fig = plt.figure() Deen CRC rete emetsty| pit.show() OUTPUT: 3D Plot | } 075 } 050 to2s } 0.00 {0.25 }-0.50 }-075 4S ° 2 Dae WATS re |Izs)i(eya) Tips — 7:1 Choosing the Right Plot Consider the nature of your data and the information you want to convey when selecting the appropriate plot type. Different plot types are suitable for different data types and objectives. 7.2 Using Colors Effectively Choose colors that are visually appealing, easy to distinguish, and accessible to color-blind individuals. Matplotlib provides a wide range of color palettes to choose from 7.3 Handling Missing Data Consider how missing data should be handled in your plots. You can choose to exclude missing values or display them in a specific way, such as using markers or different colors. 74 Adding Annotations and Text Annotations and text can provide additional context and insights to your plots. Matplotlib offers various functions for adding text, labels, and annotations to specific plot elements. | GRAVCHANORAPAOWAL _ Saving and Exporting Plots — 8.1 Saving Plots to File You can save your Matplotlib plots to various file formats, such as PNG, PDF, SVG, or JPEG, using the savefig() function. EXAMPLE: matplotlib.pyplot as plt [yt e ee) Ptaee irene 8.2 Exporting Plots to Different Formats Matplotlib supports exporting plots to different formats using the savefig() function. You can specify the desired file format using the file extension. EXAMPLE: DTC ioUn SCL om ood plt.plot(x, y) eaerset | ~~ Real-World Examples 9.1 Plotting Time Series Data Time series data is a common type of data encountered in data science. Matplotlib allows you to visualize and analyze time series data effectively. EXAMPLE: Cte rseC 7 SCL ye CCU eRe) Cle webs AG Cre Ocul e ee Cie ce Pease K Pye KG eaesaae| show() EXAMPLE: Time Series Plot 130 120 1984 1988 1992 1996 2000 2004 2008 2012 2016 Date — 9.2 Visualizing Categorical Data When working with categorical data, Matplotlib provides various plot types, such as bar plots, stacked bar plots, and pie charts, to effectively display and compare different categories. EXAMPLE: aac seC Ry Cae eeed ee CU Oe) Cee ee emery SBE cotary EXAMPLE: Bar Plot 120 100 80 Values 60 40 20 = nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnns | Categories ’AMICHANDRAPADWAL. — 9.3 Geospatial Data Visualization For geospatial data visualization, Matplotlin provides functionality to plot data on maps. You can display various geospatial features such as points, lines, polygons, and choropleth maps. EXAMPLE: Psbaeseae how() EXAMPLE: World Map 75 50 25 -50 -75 -150 -100 -50 0 50 100 150 oon Naam Conclusion Matplotlib is a powerful and versatile data visualization library for Python. In this guide, we covered the basics of Matplotlib and explored various plot types, customization options, and advanced features. By following this practical guide, you should now be equipped with the knowledge and tools to create visually appealing and informative plots for your data science projects. Experiment with different plot types and customization options to unleash the full potential of Matplotlib. Happy plotting!

You might also like