Applying Various Settings in plot() Function
The plot() function allows you specify multiple
settings for your chart/graph such as:
Color (line color/marker color), marker type, marker
size, and so forth.
Changing Line Color: To change
line color, you can specify the color code next to the data being plotted in
plot() function as shown below:
<matplotlib>.plot(<data1>, [,data2],
<color code>)
You can use color codes as ‘r’ for red, ‘y’ for
yellow, ‘g’ for green ‘b’ for blue.
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 10, 0.1)
y = np.cos(x)
z = np.sin(x)
plt.plot(x, y, 'g') #color
code green for ndarray a
plt.plot(x, z, 'r') #color
code green for ndarray b
plt.show()
Note: If you skip
the color information in plot(), Python will plot multiple lines in the same
plot with different colors but these colors are decided internally by Python.
Different Color Code in Python
S. No. |
Character |
Color |
1 |
‘b’ |
Blue |
2 |
‘r’ |
Red |
3 |
‘m’ |
Magenta |
4 |
‘g’ |
Green |
5 |
‘k’ |
Black |
6 |
‘y’ |
Yellow |
7 |
‘w’ |
White |
8 |
‘c’ |
Cyan
|
To change the line style, you can add following
optional argument in plot() function:
linestyle
or ls = [‘solid’ | ‘dashed’ , ‘dashdot’ , ‘dotted’]
Changing Marker Type, Size and Color:
To change marker type, its size and color, you can give following additional
optional arguments in plot() function:
marker = <valid marker type>, markersize -
<in points>, markeredgecolor = <valid color>
Table of Marker Types for Plotting
Marker |
Description |
Marker |
Description |
Marker |
Description |
‘ . ‘ |
point marker |
‘s’ |
square marker |
‘3’ |
tri_left marker |
‘ , ‘ |
Pixel
marker |
‘p’ |
pentagon
marker |
‘4’ |
tri_right
marker |
‘o’ |
Circle marker |
‘*’ |
Star marker |
‘v’ |
triangle_down |
‘+’ |
plus |
‘h’ |
hexagon1 |
‘^’ |
triangle_up |
‘x’ |
X marker |
‘H’ |
Hexagon2 |
‘<’ |
triangle_left |
‘D’ |
diamond |
‘1’ |
tri_down |
‘>’ |
triangle_right |
‘d’ |
thin_diamond |
‘2’ |
tri_up |
‘|’, ‘ _ ‘ |
vline, hline markers |
Example
import numpy as np
import matplotlib.pyplot as plt
a = [1,2,3,4]
b = [2,4,6,8]
plt.plot(a,b, 'k', marker = 'o', markersize = 8,
markeredgecolor = 'yellow')
plt.show()
Scatter Chart: The scatter chart is a graph of plotted
points on two axes that show the relationship between two sets of data.
It can be created through two functions of pyplot
library:
(i) plot() function
(ii) scatter() function
In plot() function, whenever you specify market
type/style, whether with color or without color, and do not give linestyle
argument, plot() will create the scatter chart.
So with marker type specified plot() creates the
scatter chart in the absence of linestyle.
Example of different scatter chart:
Q. Create an array in the range 1 to 20 with values 1.25 apart. Another array contains the log values of the elements in first array.
Solution: According to
question
import
numpy as np import
matplotlib.pyplot as plt a
= np.arange(1, 20, 1.15) b
= np.log(a) print(a) |
import
numpy as np import
matplotlib.pyplot as plt a
= np.arange(1, 20, 1.15) b
= np.log(a) print(b) |
OUTPUT [
1. 2.15 3.3
4.45 5.6 6.75
7.9 9.05 10.2 11.35 12.5
13.65 14.8
15.95 17.1 18.25 19.4 ] |
OUTPUT 0. 0.76546784 1.19392247 1.4929041 1.7227666
1.9095425 2.06686276 2.20276476 2.32238772 2.42921774
2.52572864 2.61373952 2.69462718 2.76945883 2.83907846 2.90416508
2.96527307] |
(i) Create a plot of first vs second array; specify
the x-axis (containing first array’s values) title as ‘Random Values’ and y-axis
title as ‘Logarithm Values’.
Solution:
import
numpy as np import
matplotlib.pyplot as plt a =
np.arange(1, 20, 1.15) b =
np.log(a) plt.plot(a,b) plt.xlabel('Random
Values') plt.ylabel('Logarithm
Values') plt.show()
|
OUTPUT |
(ii) Create a third array that stores the COS values
of first array and then plot both the second and third arrays vs first array.
The cos values should be plotted with a dashdotted line.
import
numpy as np import
matplotlib.pyplot as plt a =
np.arange(1, 20, 1.15) b =
np.log(a) c =
np.cos(a) plt.plot(a,b) plt.plot(a,c, linestyle = 'dashdot') plt.show() |
OUTPUT |
(iii) Change the marker type as a circle with blue
color in second array.
import
numpy as np import
matplotlib.pyplot as plt a =
np.arange(1, 20, 1.15) b =
np.log(a) c =
np.cos(a) plt.plot(a,b) plt.plot(a,c,'bo', linestyle = 'dashdot') plt.show()
|
OUTPUT |
(iv) Create scatter chart as this: second array data points as blur small diamonds, third array data points as black circles.
import
numpy as np import
matplotlib.pyplot as plt a =
np.arange(1, 20, 1.15) b =
np.log(a) c =
np.cos(a) plt.plot(a,b,
'bd') plt.plot(a,c,'ro')
plt.show() |
OUTPUT |
Specifying varying colors and size for data
points: Scatter() allows you to specify different size and
colors for data points.
Example:
import numpy as np
import matplotlib.pyplot as plt
ar1 = np.linspace(-1,1,5) # ar1 with 5 data points created
ar2 = np.exp(ar1) #
ar2 has 5 data points created
x = ['r', 'b', 'm', 'g', 'k'] # x is a sequence of color
y = [20, 60, 100, 45, 25] #
y is a sequence of sizecolor
plt.scatter(ar1, ar2, c=x, s=y)
plt.show()
OUTPUT
Note: Every students write your name and class in comment box.
Thank You!!!
No comments:
Post a Comment
Please do not any spam in the comment box.