In [1]:
import matplotlib
matplotlib.__version__


Out[1]:
'1.5.3'

使用pyplot前,必须先导入Numpy库


In [2]:
import numpy as np
import matplotlib.pyplot as plt

# 折线图
def simple_line_plot(x,y,figure_no):
    plt.figure(figure_no)
    plt.plot(x,y)
    plt.xlabel('x values')
    plt.ylabel('y values')
    plt.title('Simple Line')

In [3]:
# 绘制一个点图,点使用o,红色使用r
def simple_dots(x,y,figure_no):
    plt.figure(figure_no)
    plt.plot(x,y,'or')
    plt.xlabel('x values')
    plt.ylabel('y values')
    plt.title('Simple Dots')

In [4]:
# 绘制散点图
def simple_scatter(x,y,figure_no):
    plt.figure(figure_no)
    plt.scatter(x,y)
    plt.xlabel('x values')
    plt.ylabel('y values')
    plt.title('Simple scatter')

In [5]:
# 绘制带不同标签的颜色的散点图
def scatter_with_color(x,y,labels,figure_no):
    plt.figure(figure_no)
    plt.scatter(x,y,c=labels)
    plt.xlabel('x values')
    plt.ylabel('y values')
    plt.title('Scatter with color')

In [6]:
if __name__=="__main__":
    plt.close('all')
    # x,y
    x = np.arange(1,100,dtype=float)
    y = np.array([np.power(xx,2) for xx in x])
    
    figure_no = 1
    simple_line_plot(x,y,figure_no)
    plt.show()



In [7]:
figure_no +=1
simple_dots(x,y,figure_no)
plt.show()



In [8]:
# 绘制散点图
x = np.random.uniform(size=100)
y = np.random.uniform(size=100)

figure_no+=1
simple_scatter(x,y,figure_no)
plt.show()



In [9]:
figure_no+=1
labels = np.random.randint(2,size=100)
scatter_with_color(x,y,labels,figure_no)
plt.show()



In [10]:
plt.close('all')
# 给x和y轴添加标签
def x_y_axis_label(x,y,x_labels,y_labels,figure_no):
    plt.figure(figure_no)
    plt.plot(x,y,'+r')
    plt.margins(0.2)
    plt.xticks(x,x_labels,rotation='vertical')
    plt.yticks(y,y_labels)

In [11]:
x = np.array(range(1,6))
y = np.array(range(100,600,100))

x_label = ['element1','element2','element3','element4','element5']
y_label = ['weight1','weight2','weight3','weight4','weight5']

x_y_axis_label(x,y,x_label,y_label,1)
plt.show()



In [12]:
# 热图
def heat_plot(x,figure_no):
    plt.figure(figure_no)
    plt.pcolor(x)
    plt.colorbar()

In [13]:
x = np.random.normal(loc=0.5,scale=0.2,size=(10,10))
heat_plot(x,2)
plt.show()