Create Simple Graph in python using matplotlib.pyplot
Matplotlib : Simple Graph
Output.
Instead of using full color name you can also pass the first word of color name , like for red color line pass just " r "
If you don't know about Matplotlib then you can click here .
Hey Guys
In this Article I am going to show you how you can create Simple graphs in python using matplotlib.pyplot .
so a simple graph is just a representation of you data using a line which moves upward or downwards according to the values passed on X and Y axis.
1. create a simple graph .
CODE.
# simple graph using matplotlib import matplotlib.pyplot as plt x = [2,5,8,9] y = [4,6,9,10] # ploting our canvas plt.plot(x,y) # showing what we plotted plt.show()
Output.The above code will open a small GUI window in which you can see various buttons which can help you to do some basic operations on the graph .
.
Working of code .
In the first line i imported matplotlib.pyplot as plt ( as you know we have to import library before working on it ).
in the line 2 & 3 we created 2 list of name X and Y which are used as an argument while creating graph .
Note . ( the length of both X and Y list should be same always if not then it generates errors)
after that i called a plot which is used to create a simple bar graph in Matplotlib .
and using plt.show method the graph will show .
2. Adding title , Label of axis .
CODE.
1 2 3 4 5 6 7 8 9 10 11 12 | import matplotlib.pyplot as plt x = [2,5,8,9] y = [4,6,9,10] # ploting our canvas plt.plot(x,y) # Adding title and label of axis plt.title('Simple Plot') plt.xlabel('X Axis') plt.ylabel('Y Axis') # showing what we plotted plt.show() |
3. Line color, width and style.
CODE.
1 2 3 4 5 6 7 8 | import matplotlib.pyplot as plt x = [2,5,8,9] y = [4,6,9,10] # ploting our canvas plt.plot(x,y,'green',linestyle='--',label='line one',linewidth=3) plt.legend() plt.show() |
Output.
and for black pass " k " .
.legend( ) ?
The best part of matplotlib.pyplot is that the method names and attributes are user friendly means any one can understand the working of a particular method just by reading its name but what is the use of Legend () method because its name is not look very useful to us to understand its working .
So let me help you , if you see the last image of graph you can see a rounded corner box at top left corner which helps you to understand which color line denotes which data .
legend method is very helpful when you are presenting more then one data in a single graph .
line styles available are: '-', '--', '-.', ':', 'None', ' ', '', 'solid', 'dashed', 'dashdot', 'dotted' .
4. Plotting two or more graphs.
CODE.
1 2 3 4 5 6 7 8 9 10 11 12 13 | import matplotlib.pyplot as plt x1 = [5,8,10] y1 = [12,16,6] x2 = [6,9,11] y2 = [6,15,7] # ploting graph 1 plt.plot(x1,y1,'g',label='line one') # ploting graph 2 plt.plot(x2,y2,'r',label='line two') plt.legend() plt.show() |
Output.
As you can see now we have two lines which represents two different data and the legend method is helpful for finding which line represents which data .
This blog is contributed by Yogesh singh .
Please share your feedback and queries in comment section
and share it with your friends .
follow me on instagram
Thank you.
Nice post
ReplyDelete