Types of Plotting

Types of Plotting

In [1]:
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
In [2]:
def listAttr(name, s = None):
    if not s:
        return [item for item in dir(name) if not item.startswith("_")]
    return [item for item in dir(name) if (not item.startswith("_")) and s.lower() in item.lower()]
    pass

Scatter Plot

In [3]:
plt.scatter([1, 2, 3, 4], [2, 5, 6, 4])
plt.xlabel('X axis')
plt.ylabel('Y axis')
#plt.show()
plt.savefig('scatter.png')

Bar plot

In [4]:
plt.bar([1, 2, 3, 4], [2, 5, 6, 4])
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
In [5]:
plt.bar([1, 2, 3, 4], [2, 5, 6, 4], width=0.2)
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
In [6]:
plt.bar([1, 2, 3, 4], [2, 5, 6, 4], width=0.2, align='edge')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()

Subplot

A figure with one subplot

In [7]:
#help(np.linspace)
In [8]:
np.linspace(-10, 10, 25)
Out[8]:
array([-10.        ,  -9.16666667,  -8.33333333,  -7.5       ,
        -6.66666667,  -5.83333333,  -5.        ,  -4.16666667,
        -3.33333333,  -2.5       ,  -1.66666667,  -0.83333333,
         0.        ,   0.83333333,   1.66666667,   2.5       ,
         3.33333333,   4.16666667,   5.        ,   5.83333333,
         6.66666667,   7.5       ,   8.33333333,   9.16666667,
        10.        ])
In [9]:
x = np.linspace(-10, 10, 25)
y1 =  x ** 2
y2 = - x ** 2 + 50
In [10]:
print(x)
print(y1)
print(y2)
[-10.          -9.16666667  -8.33333333  -7.5         -6.66666667
  -5.83333333  -5.          -4.16666667  -3.33333333  -2.5
  -1.66666667  -0.83333333   0.           0.83333333   1.66666667
   2.5          3.33333333   4.16666667   5.           5.83333333
   6.66666667   7.5          8.33333333   9.16666667  10.        ]
[100.          84.02777778  69.44444444  56.25        44.44444444
  34.02777778  25.          17.36111111  11.11111111   6.25
   2.77777778   0.69444444   0.           0.69444444   2.77777778
   6.25        11.11111111  17.36111111  25.          34.02777778
  44.44444444  56.25        69.44444444  84.02777778 100.        ]
[-50.         -34.02777778 -19.44444444  -6.25         5.55555556
  15.97222222  25.          32.63888889  38.88888889  43.75
  47.22222222  49.30555556  50.          49.30555556  47.22222222
  43.75        38.88888889  32.63888889  25.          15.97222222
   5.55555556  -6.25       -19.44444444 -34.02777778 -50.        ]
In [11]:
fig, ax = plt.subplots()
ax.plot(x, y1)
ax.plot(x, y2)
Out[11]:
[<matplotlib.lines.Line2D at 0x2c08f7990a0>]
In [12]:
fig
Out[12]:
In [13]:
ax
Out[13]:
<AxesSubplot:>
In [14]:
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(x, y1)
ax.plot(x, y2)
Out[14]:
[<matplotlib.lines.Line2D at 0x2c08f70b0d0>]
In [15]:
ax
Out[15]:
<AxesSubplot:>
In [16]:
fig, ax = plt.subplots(figsize=(6, 4))

#matplotlib.pyplot.step(x, y, *args, where='pre', data=None, **kwargs)
ax.step(x, y1)
ax.step(x, y2)
Out[16]:
[<matplotlib.lines.Line2D at 0x2c08f7464c0>]
In [17]:
fig, axes = plt.subplots( nrows=2, ncols=3) 
In [18]:
fig
Out[18]:
In [19]:
axes
Out[19]:
array([[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>],
       [<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>]], dtype=object)
In [20]:
# plot a line, implicitly creating a subplot(111)
plt.plot(x)
plt.figure(figsize=(6, 3))  

# now create a subplot which represents the top plot of a grid
# with 2 rows and 1 column. Since this subplot will overlap the
# first, the plot (and its axes) previously created, will be removed
plt.subplot(211)
Out[20]:
<AxesSubplot:>
In [21]:
# plot a line, implicitly creating a subplot(111)
plt.plot(x)
plt.figure(figsize=(6, 3))  
plt.subplot(221)
Out[21]:
<AxesSubplot:>
help(plt.subplot)
In [22]:
plt.subplot(221)

# equivalent but more general
ax1=plt.subplot(2, 2, 1)

# add a subplot with no frame
ax2=plt.subplot(222, facecolor='blue')

# add a polar subplot
ax3=plt.subplot(223, projection='polar')

# add a red subplot that shares the x-axis with ax1
ax4= plt.subplot(224, sharex=ax1, facecolor='red')
In [24]:
plt.subplot(221)

# equivalent but more general
ax1=plt.subplot(2, 2, 1)

# add a subplot with no frame
ax2=plt.subplot(222, facecolor='blue')

# add a polar subplot
ax3=plt.subplot(223, projection='polar')

# add a red subplot that shares the x-axis with ax1
ax4= plt.subplot(224, sharex=ax1, facecolor='red')

# delete ax2 from the figure
#plt.delaxes(ax3)

# add ax2 to the figure again
#plt.subplot(ax3)
In [25]:
names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]

plt.figure(figsize=(15, 3))  

plt.subplot(131)
plt.bar(names, values)

plt.subplot(133)
plt.plot(names, values)

plt.subplot(132)
plt.scatter(names, values)
Out[25]:
<matplotlib.collections.PathCollection at 0x2c0925d67c0>
In [26]:
names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]

plt.figure(figsize=(15, 3))  

plt.subplot(131)
plt.bar(names, values)

plt.subplot(133)
plt.plot(names, values)

plt.subplot(132)
plt.scatter(names, values)

plt.suptitle('Categorical Plotting')
#plt.show()
plt.savefig('test.png')
In [27]:
x = np.arange(-5, 6, 1)
y1 = 20 * x - 50
y2 = 100 - 20 * x
y3 = x ** 3 - x ** 2
In [28]:
#create Figure and Axis
fig, ax = plt.subplots()

#in the Axis, plot graphs
ax.plot(x, y1, color="blue", label="y(x)")
ax.plot(x, y2, color="red", label="y'(x)")
ax.plot(x, y3, color="green", label="y''(x)")

#set Axis x and y labels
ax.set_xlabel("x")
ax.set_ylabel("y")

#add legend for the graph
ax.legend()
Out[28]:
<matplotlib.legend.Legend at 0x2c0926cbca0>

Save egraph

In [29]:
fig.savefig("xv-plotting.pdf")
In [30]:
fig.savefig("xv-plotting.png")
In [31]:
y = [1,4,3,8,10,6,13,14,16,10]
x = np.arange(10)
plt.plot(x, y, label='$y = numbers')
plt.title('Legend inside')
ax.legend()
#plt.show()

fig.savefig('plot.png')
In [32]:
y = [1,4,3,8,10,6,13,14,16,10]
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='$y = numbers')
plt.title('Legend inside')
ax.legend()
#plt.show()

fig.savefig('plot.png')
In [33]:
x = [1, 2, 4]
y = [2, 4, 4.5]
#plt.figure(figsize=(10, 5))
plt.plot(x, y)
plt.savefig("out.png")

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