python散點條形圖的繪制
<pre>
import numpy as np
import matplotlib.pyplot as plt
引用樣式
plt.style.use('ggplot')
生成200個隨機數(shù)據(jù)
x=np.random.randn(200)
y=x+np.random.randn(200)*0.5
設置坐標系的位置
margin_border=0.1
width=0.6
margin_between=0.02
height=0.2
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=margin_between+margin_border+width
bottom_y=margin_border
height_y=width
width_y=height
生成rect對象
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_s=plt.axes(rect_s)
axScatter_x=plt.axes(rect_x)
axScatter_y=plt.axes(rect_y)
上下兩個坐標系的x,y坐標取消掉
axScatter_x.set_xticks([])
axScatter_y.set_yticks([])
畫出主坐標的散點圖
axScatter_s.scatter(x,y,color='b')
設置柱狀圖的柱子寬度
bin_width=0.25
取x,y的絕對值的最大值
xymax=np.max([np.max(np.fabs(x)),np.max(np.fabs(y))])
確定坐標的的寬度
lim=int(xymax/bin_width+1)*bin_width
設置xy坐標軸的最大最小坐標
axScatter_s.set_xlim(-lim,lim)
axScatter_s.set_ylim(-lim,lim)
生成柱狀圖的橫坐標上的點
bins=np.arange(-lim,lim+bin_width,bin_width)
畫出柱狀圖x
axScatter_x.hist(x,bins=bins)
畫出柱狀圖y
axScatter_y.hist(y,bins=bins,orientation='horizontal')
設置x,y坐標的極限值
axScatter_x.set_xlim(axScatter_s.get_xlim())
axScatter_y.set_ylim(axScatter_s.get_ylim())
設置坐標的title,但是位置出現(xiàn)的地方需要調(diào)整
plt.title('scatter and hist')
顯示出該圖像
plt.show()
</pre>