Python学习
积分图实战
下面是Demo
#积分图
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Polygon
def func(x):
return -(x-2)*(x-8)+40
x = np.linspace(0,10)
y = func(x)
fig,ax=plt.subplots()
plt.plot(x,y,'r',linewidth=2)
a = 2
b = 9
ax.set_xticks([a,b])
#ax.set_yticks([])
ax.set_xticklabels(['$a$','$b$'])
plt.figtext(0.9,0.05,'$x$')
plt.figtext(0.1,0.9,'$y$')
#灰色阴影的绘制
ix = np.linspace(a,b)
iy = func(ix)
ixy = zip(ix,iy)
verts=[(a,0)]+list(ixy)+[(b,0)]
poly=Polygon(verts,facecolor='0.5',edgecolor='0.1')
ax.add_patch(poly)
#数学公式添加
x_math=(a+b)*0.5
y_math=35
plt.text(x_math,y_math,r'$\int_a^b(-(x-2)*(x-8)+40)dx$',fontsize=15,horizontalalignment='center')
#直接对齐的中心函数:horizontalalignment='center'
plt.ylim(ymin=25)
plt.show()
![运算结果](https://img-blog.csdnimg.cn/20201024100024182.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2lwcXFpcHFx,size_16,color_FFFFFF,t_70#pic_center)
散点图实战
下面是Demo
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
x = np.random.randn(200)
y = np.random.randn(200)*0.5
margin_border=0.1
width=0.6
height=0.2
margin_between=0.02
left_s = margin_border
bottom_s = margin_border
height_s=width
width_s=width
left_x = margin_border
bottom_x = margin_border+width+margin_between
height_x = height
width_x = width
left_y = bottom_x
bottom_y = margin_border
height_y = width
width_y = height
plt.figure(1,figsize=(8,8))
rect_s = [left_s,bottom_s,width_s,height_s]
rect_x = [left_x,bottom_x,width_x,height_x]
rect_y = [left_y,bottom_y,width_y,height_y]
axScatter=plt.axes(rect_s)
axHisX=plt.axes(rect_x)
axHisY=plt.axes(rect_y)
axHisX.set_xticks([])#去掉多余的标签
axHisY.set_yticks([])
axScatter.scatter(x,y)#绘制散点图
bin_width=0.25
xymax= np.max([np.max(np.fabs(x)),np.max(np.fabs(y))])
lim=int(xymax/bin_width+1)*bin_width
axScatter.set_xlim(-lim,lim)
axScatter.set_ylim(-lim,lim)
bins = np.arange(-lim,lim+bin_width,bin_width)
axHisX.hist(x,bins=bins)
axHisY.hist(y,bins=bins,orientation='horizontal')
axHisX.set_xlim(axScatter.get_xlim())
axHisY.set_ylim(axScatter.get_ylim())
plt.title('Scatter and hist')
plt.show()