Data Visualization using PyPlot
Visualization gives you answers to questions you didn’t know you had.. – Ben Schneiderman
An editorial approach to visualization design requires us to take responsibility to filter out the noise from the signals, identifying the most valuable, most striking or most relevant dimensions of the subject matter in question. – Andy Kirk
Data visualization doesn’t live in an ethereal dimension, separated from the data. When there’s a large number of pie-charts in a report or a presentation, there is something wrong in the organization, and it’s not the pie. A pie chart is a potential symptom of lack of data analysis skills that have to be resolved. – Jorge Camoes
Pictures playing an important role in representing data. As we all are aware that pictures giving a more and more clear understanding of any kind of data or complex problems. Some of the images help to understand the structure or patterns of data flow and execution.
Basic components of Graph
- Figure or chart area: The entire area covered by the graph is known as a figure. It can be also considered as a canvas or chart area also.
- Axis: These are the number of lines generated on the plot. Basically, there are two axis X and Y-axis.
- Artist: The components like text objects, Line 2D objects, collection objects, etc.
- Titles: There are few titles involved with your charts such as Chart Title, Axis title, etc.
- Legends: Legends are the information that represents data with lines or dots.
Steps how to create graphs using matplotlib
Step 1 Installation of matplotlib
Step 2 import module
- Without instance: import matplotlib.pyplot
- With instance: import matplotlib.pyplot as mpp
Step 3 Choose desired plot type (graph type)
Step 4 Give proper labels to axis, categories
Step 5 Add data points
Step 6 Add more functionality like colors, sizes etc
- Line plot
- Bar graph
- Pie chart
- Scatter chart
Creating a Line chart or Plotting lines
- plot(x,y,color,others): Draw lines as per specified lines
- xlabel(“label”): For label to x-axis
- ylabel(“label”): For label to y-axis
- title(“Title”): For title of the axes
- legend(): For displaying legends
- show() : Display the graph
Now observe the following code:
import matplotlib.pyplot as mpp mpp.plot(['English','Maths','Hindi'],[88,90,94],'Red') mpp.xlabel('Subjects') mpp.ylabel('Marks') mpp.title('Progress Report Chart') mpp.show()
|Line plot in Python 3.8.3|
import matplotlib.pyplot as mpp o=[5,10,15,20] r_india=[30,80,120,200] mpp.plot(o,r_india,'Red') r_aust=[25,85,100,186] mpp.plot(o,r_aust,'Yellow') mpp.xlabel('Runs') mpp.ylabel('Overs') mpp.title('Match Summary') mpp.show()
|Multiline chart using Python 3.8.3|
import matplotlib.pyplot as mpp overs=[5,10,15,20] runs=[30,80,120,200] mpp.bar(runs,overs,width=30, label='Runs',color='r') mpp.xlabel('Runs') mpp.ylabel('Overs') mpp.title('Match Summary') mpp.legend() mpp.show()
|Bar Graph in python 3.8.3|