写在前面
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号
plt.gcf( )与plt.gca( )
当前的图表和子图可以使用plt.gcf()和plt.gca()获得,分别表示Get Current Figure和Get Current Axes。在pyplot模块中,许多函数都是对当前的Figure或Axes对象进行处理,比如说:plt.plot()实际上会通过plt.gca()获得当前的Axes对象ax,然后再调用ax.plot()方法实现真正的绘图。
画出带四个象限的坐标系(对图表的外边框进行设置)
x = np.linspace(-10, 10, 1000)
y = np.sin(x)
plt.plot(x,y)
ax = plt.gca()
ax.spines['right'].set_color(None)
ax.spines['left'].set_color('y')
ax.spines['top'].set_color(None)
ax.spines['bottom'].set_color('g')
#把x轴的刻度设置为‘bottom’
#把y轴的刻度设置为‘left’
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
#设置bottom对应到0点
#设置left对应到0点
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
plt.figure():创建figure对象
Figure对象是后续绘图操作的总容器。
如果不创建figure对象,直接调用接下来的plot()进行绘图,matplotlib会自动创建一个figure对象,但是事先创建figure对象则可以对它做更精细的设定。
figure(num=None,figsize=None,facecolor=None,edgecolor=None,frameon=True)
num:图形编号或名称,数字为编号,字符串为名称。
figsize:指定figure的宽和高,单位为英寸。
dpi:指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80。
facecolor:背景颜色。
edgecolor:边框颜色。
frameon:是否显示边框。
#facecolor图形颜色
#edgecolor边缘颜色
x = np.linspace(-5,5,1000)
y = np.cos(x)
plt.figure("Apple",figsize = (5,3),dpi = 80,facecolor = "c",edgecolor = "red",frameon=True)
plt.plot(x,y)
plt.title("dpi = 80")
plt.show()
plt.savefig()
plt.figure('apple',figsize=(8,8))
x = np.linspace(-10, 10, 1000)
y = np.sin(x)
plt.figure(dpi=100)
plt.plot(x,y)
plt.savefig('C:\\Users\\Administrator\\Desktop\\数据可视化.jpg')
plt.bar( )与plt.barh( )
x = np.arange(10)
data = np.random.randint(1,11,10)
data
#绘制条形图
plt.bar(x,data)
plt.show()
x = np.arange(10)
data = np.random.randint(1,11,10)
data
plt.barh(x,data,label='asd')
plt.legend(loc = 1)
plt.title('hjjjjh')
plt.show()
plt.bar(x,data,width=0.5)
plt.bar(x,data,facecolor='g',edgecolor='r',lw=5,alpha = 0.3)
plt.bar(x,-data,facecolor='g',edgecolor='r',lw=5,alpha = 0.3)
plt.bar(x,data,facecolor='g',edgecolor='r',hatch='\/',lw=3)
index = np.arange(5)
data1 = [3,4,6,7,8]
data2=[11,23,45,6,4]
data3=[12,6,21,8,9]
a = 0.3
plt.title('multi bar chart')
plt.bar(index, data1,a,color='r',label = 'a',alpha = 0.8)
plt.bar(index+a, data2,a,color='g',label = 'b')
plt.bar(index+2*a, data3,a,color='b',label = 'c')
plt.legend(loc=1)
index = np.arange(5)
data1 = [3,4,6,7,8]
data2=[11,23,45,6,4]
data3=[12,6,21,8,9]
a = 0.3
plt.title('multi bar chart')
plt.barh(index, data1,a,color='r',label = 'a',alpha = 0.8)
plt.barh(index+a, data2,a,color='g',label = 'b')
plt.barh(index+2*a, data3,a,color='b',label = 'c')
plt.legend(loc=1)
index = np.arange(5)
data1 = np.array([3,4,6,7,8])
data2=np.array([11,2,4,6,4])
data3=np.array([12,6,2,8,9])
a = 0.3
plt.title('multi bar chart')
plt.bar(index, data1,hatch='\/',color='r',label = 'a',alpha = 0.8)
plt.bar(index, data2,hatch='/',bottom = data1,color='g',label = 'b')
plt.bar(index, data3,hatch='+/',bottom = (data1+data2),color='b',label = 'c',alpha = 0.8)
plt.legend(loc=1)
饼图
datas = [33, 16, 56, 90, 55]
plt.pie(datas)
colors = ['r','green','blue','pink', 'yellow']
labels = ['xiaomi', 'huawei','apple','oppo','vivo']
plt.pie(datas, colors = colors, labels = labels)