Three, Matplotlib data visualization-line chart

1. Concept

A line chart is a graph formed by connecting various data with straight lines. It is often used to observe the trend of data over time , such as stock prices, temperature changes, and so on.

 

 2. Draw a simple line chart

Linspace is a function in numpy that divides the interval into equal parts. For example, numpy.linspace(-10,10,100) divides the interval from -10 to 10 into 100 parts for value.

import numpy as np
import matplotlib.pyplot as plt 

x=np.linspace(-10,10,5)
y=x**2

plt.plot(x,y)

plt.show()

Three, some configurations of the line chart function

The configuration of the line chart function is similar to that of the scatter chart. Since the Shanghai Composite Index 000001.SH.csv file was not found, I randomly generated a 000001.SH.csv file . The plot() function is to generate a normal line chart, the plot_date() function generates a line chart with the date on the horizontal axis, and the default generated in the plot_date() function is a point chart. You need to add fmt='-' to turn it into a line chart . color specifies the color of the point, and marker specifies the shape of the point. You can draw multiple line graphs in one graph, as shown in the figure below.

import numpy as np
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates

#随机生成000001.SH.csv文件,保存到当前目录
arr=[]
header='Date,Open,High,Low,Close'

for month in range(1):
    for day in range(20):
        inta=sorted(np.random.randint(1,100,size=4))
        temp=[str(month+1)+'/'+str(day+1)+'/2021',inta[1],inta[3],inta[0],inta[2]]
        arr.append(temp)

np.savetxt('000001.SH.csv',arr,fmt='%s',delimiter=',',comments='',header=header)

#打开000001.SH.csv文件
date,open,high,low,close=np.loadtxt('000001.SH.csv',delimiter=',',
                            skiprows=1,unpack=True,dtype=str)

plt.plot_date(date,open,fmt='-',color='green',marker='<')
plt.plot_date(date,close,fmt='-',color='red',marker='o')

plt.show()

 

Four, homework

1. Draw the sine function image with X value [0,10]

import numpy as np
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates

x=np.linspace(0,10,100)
plt.plot(x,np.sin(x),'-',color='red',marker='')
plt.show()

2. Read Open, High, Low, Close of 000001.SH and draw them on a picture

We still use the 000001.SH.csv file we generated to generate the image .

import numpy as np
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates

#随机生成000001.SH.csv文件,保存到当前目录
arr=[]
header='Date,Open,High,Low,Close'

for month in range(1):
    for day in range(20):
        inta=sorted(np.random.randint(1,100,size=4))
        temp=[str(month+1)+'/'+str(day+1)+'/2021',inta[1],inta[3],inta[0],inta[2]]
        arr.append(temp)

np.savetxt('000001.SH.csv',arr,fmt='%s',delimiter=',',comments='',header=header)

#打开000001.SH.csv文件
date,open,high,low,close=np.loadtxt('000001.SH.csv',delimiter=',',
                            skiprows=1,unpack=True,dtype=str)

plt.plot_date(date,open,fmt='-',color='green',marker='<')
plt.plot_date(date,high,fmt='-',color='blue',marker='*')
plt.plot_date(date,low,fmt='-',color='black',marker='x')
plt.plot_date(date,close,fmt='-',color='red',marker='o')

plt.show()

 

 

 

Guess you like

Origin blog.csdn.net/qq_40836442/article/details/112997765