1.折线图
#折线图
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
%matplotlib
plt.figure(1)
x = np.linspace(-3,3)
y=2*pow(x, 3)+5*x**2
plt.plot(x, y,ls=':',marker='o',markersize=5,color='red')
plt.grid(True)
plt.show()
2.柱状图
#柱状图
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib #导入库
matplotlib.matplotlib_fname() #显示配置文件
%matplotlib inline
%matplotlib
plt.rcParams['font.sans-serif']=['SimHei'] #中文乱码
sign = list('abcde')
# sign = np.arange(5)
data = [70, 89, 79, 95, 65]
plt.bar(sign, data,
width=w , color='green', # 宽度, 内部填充色
edgecolor='y', hatch='/',
tick_label=['小明','小华','小红','小强','小黑'],) # 边缘色, 内部填充图案/
# plt.bar(sign, data)
plt.ylim([0, 100])
for x, y in enumerate(data):
plt.text(x, y+0.03, '{}'.format(y), ha='center', fontsize=14)
plt.show()
3.多图制作
#多图制作
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
%matplotlib
x = np.linspace(-4, 4, 120)
fig = plt.figure() # 创建一个figure对象 或 fig=plt.gcf()
ax1 = fig.add_subplot(2, 2, 1) # 2行x 2列图形的第一个子图
ax1.plot(x, 2*np.power(x,3)+ 5*x,color='blue')
ax1.set_title('y1 = 2*np.power(x,3)+ 5*x',fontsize=10)
ax1.grid(True)
ax2 = fig.add_subplot(2, 2, 2) # 2行x 2列图形的第二个子图
ax2.plot(x, 2*x**2 + x ,color='red')
ax2.set_title('y2 = 2*x**2 + x ',fontsize=10)
ax2.grid(True)
ax3 = fig.add_subplot(2, 2, 3) # 2行x 2列图形的第三个子图
ax3.plot(x, 2*x,color='yellow')
ax3.set_title('y3 = 2*x',fontsize=10)
ax3.grid(True)
ax4 = fig.add_subplot(2, 2, 4) # 2行x 2列图形的第四个子图
ax4.plot(x, x**3)
ax4.set_title('y4 =x**3',fontsize=10)
ax4.grid(True)
4.饼图
#饼图
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
%matplotlib
rate = [1060, 1300, 2000, 2800]
labels = ['Mi', 'Oppo', 'Apple', 'Huawei']
explode = (0.02, 0.02, 0.02, 0.02) # explode设置各部分分割出来的间隙
patches, ltext, ptext = plt.pie(rate, explode=explode, labels=labels,
autopct='%.1f%%',shadow=True,startangle=0)
for x in ltext:
x.set_size(20) # 设置标注文字大小
for x in ptext:
x.set_size(24) # 设置百分比文字大小
plt.axis('equal') # 设置x/y轴单位长度相等,以显示正圆
plt.legend()