# Python—–数据可视化（matplotlib）（二）

## 1.添加注释

plot.annotate('this is the bottom',xy=(0,1),xytext(0,20),
arrowprops=dict(facecolor = 'r',frac=0.2,headwidth=30,width=20))

xy=箭头起始位置

xytext=注释位置

facecolor=箭头颜色

frac=箭头比例

width=箭身宽度

## 2.添加文本

plot.text(-2,40,'text test',family='fantasy',size=20,color='g',style='oblique',weight='light')

family=字体

style=斜体

weight=粗体

## 3.添加公式

plot.text(2,4,r"/alpha_i /beta_j /pi /lambda /omega",size=25)

## 4.区域填充

plt.fill(x,y1,'b',alpha=0.3)

plt.fill(x,y2,'r',alpha=0.3)

alpha=透明度

plt.fill_between(x, y1, y2, where=y1 > y2, facecolor='b', interpolate=True)
plt.fill_between(x, y1, y2, where=y1 < y2, facecolor='g', interpolate=True)


## 5.生成形状

xy1=np.array([0.2,0.2])
xy2=np.array([0.2,0.8])
xy3=np.array([0.8,0.2])
xy4=np.array([0.8,0.8])

circle=mpatches.Circle(xy1,0.05)

rect=mpatches.Rectangle(xy2,0.2,0.1,color='r')

polygon=mpatches.RegularPolygon(xy3,5,0.1,color='g')

ellipse=mpatches.Ellipse(xy4,0.4,0.2,color='y')
ax.add_patch(ellipse)

## 6.绘制极坐标

theta = [0, np.pi / 2, np.pi, 3 * np.pi / 2, 2 * np.pi]

ax = plt.subplot(111, projection='polar')

ax.plot(theta, r, color='r', linewidth=3)

## 7.绘制散点-条形图

import numpy as np
import matplotlib.pyplot as plt

plt.style.use('ggplot')

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_border + width + margin_between
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())
axHisX.set_ylim(axScatter.get_ylim())
plt.show()


Vieu3.3主题

Q Q 登 录