Single Line Plots

Single Line Plots

In [1]:
import matplotlib.pyplot as plt
import numpy as np
In [2]:
x = np.array([4, 5, 3, 1, 6, 7])
In [3]:
plt.plot(x)
plt.show()

Create x with numpy arange()

In [4]:
x = np.arange(25)

If only one value is given in plt.plot(x), first parameter x will be taken as index value like 0, 1, 2, 3.... and second parametetr will be taken as y.

In [5]:
plt.plot(x)
plt.show()
In [6]:
plt.plot(x, x**2 + 1)
plt.show()
In [7]:
[y for y in x]
Out[7]:
[0,
 1,
 2,
 3,
 4,
 5,
 6,
 7,
 8,
 9,
 10,
 11,
 12,
 13,
 14,
 15,
 16,
 17,
 18,
 19,
 20,
 21,
 22,
 23,
 24]
In [8]:
y = [(y ** 2) for y in x]
y
Out[8]:
[0,
 1,
 4,
 9,
 16,
 25,
 36,
 49,
 64,
 81,
 100,
 121,
 144,
 169,
 196,
 225,
 256,
 289,
 324,
 361,
 400,
 441,
 484,
 529,
 576]
In [9]:
plt.plot(x, y)
plt.show()
In [10]:
y = [(y**3 + 1) for y in x]
In [11]:
plt.plot(x, y)
plt.show()

Multiple plots

In [12]:
x = np.arange(10)
x
Out[12]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [13]:
plt.plot(x, x**2)
plt.plot(x, x**3)
plt.plot(x, 2*x)
plt.plot(x, 2**x)
plt.show()
In [14]:
plt.plot(x, x**2, x, x**3, x, 2*x, x, 2**x)
plt.show()
In [15]:
plt.plot(x, -x**2)
plt.plot(x, -x**3)
plt.plot(x, -2*x)
plt.plot(x, -2**x)
plt.show()
In [16]:
plt.plot(x, -x**2, x, -x**3, x, -2*x, x, -2**x)
plt.show()
In [17]:
x = np.array([[6, 2, 1, 5, 4], [7, 4, 1, 5, 2]])
x
Out[17]:
array([[6, 2, 1, 5, 4],
       [7, 4, 1, 5, 2]])
In [18]:
plt.plot(x)
plt.show()
In [19]:
x = np.random.randint(5, size=(2, 4))
x
Out[19]:
array([[2, 0, 2, 2],
       [3, 0, 4, 4]])
In [20]:
[x[0], x[1]]
Out[20]:
[array([2, 0, 2, 2]), array([3, 0, 4, 4])]
In [21]:
plt.plot([x[0], x[1]])
plt.show()
In [22]:
x[0], x[1]
Out[22]:
(array([2, 0, 2, 2]), array([3, 0, 4, 4]))
In [23]:
plt.plot(x[0], x[1])
plt.show()
In [24]:
x = np.random.randn(2, 10)
x
Out[24]:
array([[ 0.00783726,  0.07882926, -0.26440834,  0.72644349, -0.36491923,
        -0.285299  ,  0.99091752, -0.96516995, -0.04604364, -0.12154105],
       [ 0.34164713, -0.68638029,  0.72915734,  0.02139741,  0.41381231,
        -1.678068  ,  0.61621656,  0.88562197,  0.50640952,  0.31952046]])
In [25]:
plt.plot([x[0], x[1]])
plt.show()

Legends and labels

In [26]:
x = np.array([9, 1, 4, 2, 1])
x
Out[26]:
array([9, 1, 4, 2, 1])
In [27]:
y = np.array([5, 7, 3, 4, 1])
y
Out[27]:
array([5, 7, 3, 4, 1])
In [28]:
plt.plot(x, y)
plt.title("X & Y Line Graph")
plt.xlabel("X - axis")
plt.ylabel("y - axis")
plt.show()
In [29]:
plt.plot(x, y, color = "red")
plt.title("X & Y Line Graph")
plt.xlabel("X - axis")
plt.ylabel("y - axis")
plt.show()
In [30]:
plt.plot(x, y, color = "red", label = "xy")
plt.plot(y, x, color = "green", label = "yx")

plt.title("X & Y Line Graph")
plt.xlabel("X - axis")
plt.ylabel("y - axis")
plt.legend()
plt.show()
In [31]:
plt.plot(x, y, color = "red", label = "xy")
plt.plot(y, x, color = "green", label = "yx")

plt.title("X & Y Line Graph", color = "red", fontsize = 20)
plt.xlabel("X - axis", color = "blue", fontsize = 15)
plt.ylabel("y - axis", color = "green", fontsize = 15)
plt.legend()
plt.show()

Change the legend position

Location String Location Code 'best' 0 'upper right' 1 'upper left' 2 'lower left' 3 'lower right' 4 'right' 5 'center left' 6 'center right' 7 'lower center' 8 'upper center' 9 'center' 10
In [32]:
plt.plot(x, y, color = "red", label = "xy")
plt.plot(y, x, color = "green", label = "yx")

plt.title("X & Y Line Graph", color = "red", fontsize = 20)
plt.xlabel("X - axis", color = "blue", fontsize = 15)
plt.ylabel("y - axis", color = "green", fontsize = 15)

plt.legend(loc='upper left')

plt.show()

Different types of line graph

linestyle description

'-' or 'solid' solid line '--' or 'dashed' dashed line '-.' or 'dashdot' dash-dotted line ':' or 'dotted' dotted line

In [33]:
plt.plot(x, x, "-", color = "blue", label = "xx")
plt.plot(x, y, "--", color = "red", label = "xy")
plt.plot(y, x, '-.', color = "green", label = "yx")

plt.title("X & Y Line Graph", color = "red", fontsize = 20)
plt.xlabel("X - axis", color = "blue", fontsize = 15)
plt.ylabel("y - axis", color = "green", fontsize = 15)

plt.legend(loc='upper left')

plt.show()

Markers, color and line style

marker description

"." point "," pixel "o" circle "v" triangle_down "^" triangle_up "<" triangle_left ">" triangle_right "1" tri_down "2" tri_up "3" tri_left "4" tri_right "8" octagon "s" square "p" pentagon "P" plus (filled) "*" star "h" hexagon1 "H" hexagon2 "+" plus "x" x "X" x (filled) "D" diamond "d" thindiamond "|" vline "" hline 0(TICKLEFT) tickleft 1(TICKRIGHT)tickright 2(TICKUP) tickup 3(TICKDOWN) tickdown 4(CARETLEFT)caretleft 5(CARETRIGHT)caretright 6(CARETUP) caretup 7(CARETDOWN)caretdown 8(CARETLEFTBASE)caretleft (centered at base) 9(CARETRIGHTBASE)caretright (centered at base) 10(CARETUPBASE)caretup (centered at base) 11(CARETDOWNBASE)caretdown (centered at base)

In [34]:
plt.plot(x, x, "ro--", label = "xx")
plt.plot(x, y, "y<--", label = "xy")
plt.plot(y, x, 'bD-.', label = "yx")

plt.title("X & Y Line Graph", color = "red", fontsize = 20)
plt.xlabel("X - axis", color = "blue", fontsize = 15)
plt.ylabel("y - axis", color = "green", fontsize = 15)

plt.legend(loc='upper left')

plt.show()

Set the x limit and y limit

In [35]:
plt.plot(x, y, label = "xy")
plt.plot(y, x, label = "yx")

plt.title("X & Y Line Graph")
plt.xlabel("X - axis")
plt.ylabel("y - axis")
plt.legend()
plt.grid(True)

plt.xlim(0, 12)
plt.ylim(0, 12)

plt.show()

Save figure

If only image name is provided in savefig() function, figure will save in current directory. If we want to save in specific directory, we have to provide folder location and image name.

In [36]:
plt.plot(x, y, label = "xy")
plt.plot(y, x, label = "yx")

plt.title("X & Y Line Graph")
plt.xlabel("X - axis")
plt.ylabel("y - axis")
plt.legend()
plt.grid(True)

plt.xlim(0, 12)
plt.ylim(0, 12)

plt.savefig('image1.png')

plt.show()

Subplots

In [37]:
x = np.array([9, 1, 4, 2, 1])
y = np.array([5, 7, 3, 4, 1])
y1 = np.array([3, 1, 5, 9, 3])

Single subplot

In [38]:
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('A single plot')
plt.show()

Vertically stacked subplots

In [39]:
fig, (ax1, ax2) = plt.subplots(2)
fig.suptitle('Vertically stacked subplots')
ax1.plot(x, y)
ax2.plot(x, y1)
plt.show()

Horizontal stacked subplots

In plt.subplot(1, 2): first parameter represents row value which is 1, and second value represent column value which is 2.

In [40]:
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Horizontally stacked subplots')
ax1.plot(x, y)
ax2.plot(x, y1)
plt.show()

Horizontal and vertically stacked subplots

In [41]:
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)

ax1.plot(x, 'ro--')
ax1.set_title("First Graph")

ax2.plot(x, y, 'g--')
ax1.set_title("Second Graph")

ax3.plot(x, y1, color = "blue")
ax1.set_title("Third Graph")

ax4.plot(x, y**2, color = "purple")
ax1.set_title("Fourth Graph")

fig.suptitle('Horizontally stacked subplots')

plt.show()

Machine Learning

  1. Deal Banking Marketing Campaign Dataset With Machine Learning

TensorFlow

  1. Difference Between Scalar, Vector, Matrix and Tensor
  2. TensorFlow Deep Learning Model With IRIS Dataset
  3. Sequence to Sequence Learning With Neural Networks To Perform Number Addition
  4. Image Classification Model MobileNet V2 from TensorFlow Hub
  5. Step by Step Intent Recognition With BERT
  6. Sentiment Analysis for Hotel Reviews With NLTK and Keras
  7. Simple Sequence Prediction With LSTM
  8. Image Classification With ResNet50 Model
  9. Predict Amazon Inc Stock Price with Machine Learning
  10. Predict Diabetes With Machine Learning Algorithms
  11. TensorFlow Build Custom Convolutional Neural Network With MNIST Dataset
  12. Deal Banking Marketing Campaign Dataset With Machine Learning

PySpark

  1. How to Parallelize and Distribute Collection in PySpark
  2. Role of StringIndexer and Pipelines in PySpark ML Feature - Part 1
  3. Role of OneHotEncoder and Pipelines in PySpark ML Feature - Part 2
  4. Feature Transformer VectorAssembler in PySpark ML Feature - Part 3
  5. Logistic Regression in PySpark (ML Feature) with Breast Cancer Data Set

PyTorch

  1. Build the Neural Network with PyTorch
  2. Image Classification with PyTorch
  3. Twitter Sentiment Classification In PyTorch
  4. Training an Image Classifier in Pytorch

Natural Language Processing

  1. Spelling Correction Of The Text Data In Natural Language Processing
  2. Handling Text For Machine Learning
  3. Extracting Text From PDF File in Python Using PyPDF2
  4. How to Collect Data Using Twitter API V2 For Natural Language Processing
  5. Converting Text to Features in Natural Language Processing
  6. Extract A Noun Phrase For A Sentence In Natural Language Processing