2. Matplotlib draws scatter plots

2. Matplotlib draws scatter plots

2.1. Examples of height and weight

import matplotlib.pyplot as plt

height = [161,162,163,164,165]
weight = [50,60,70,80,90]

plt.scatter(height,weight)
plt.show()

Run to generate a scatter plot

In [48]: %run sandian.py

2.2. Examples of stock gains

#Stock gains
import numpy as np
import matplotlib.pyplot as plt

#Close and open data
open,close = np.loadtxt('01.csv',delimiter=',',skiprows=1,usecols=(1,4),unpack=True)
#Close and open increase
change = close-open
#Compare the difference between today’s and yesterday’s gains and draw a scatter plot to analyze the correlation
yesterday = change[:-1]
today = change[1:]

plt.scatter(yesterday,today)
plt.show()

Scatter chart, found that there is no correlation between today’s and yesterday’s gains

2.3. Parameter introduction

Common parameters

  • c: color
  • s: point size
  • alpha: transparency
  • marker: the shape of the point

 Still the stock example above, add these few parameters

#Stock gains
import numpy as np
import matplotlib.pyplot as plt

#Close and open data
open,close = np.loadtxt('01.csv',delimiter=',',skiprows=1,usecols=(1,4),unpack=True)
#Close and open increase
change = close-open
#Compare the difference between today’s and yesterday’s gains and draw a scatter plot to analyze the correlation
yesterday = change[:-1]
today = change[1:]

plt.scatter(yesterday,today,s=50,c='r',marker='*',alpha=0.5)
plt.show()
Reference: https://cloud.tencent.com/developer/article/1183189 2. Matplotlib draws scatter plots-Cloud + Community-Tencent Cloud