面積圖谈宛、填圖次哈、餅圖
plt.plot.area()
plt.fill(), plt.fill_between()
plt.pie()
# 面積圖
fig,axes = plt.subplots(2,1,figsize = (8,6))
df1 = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df2 = pd.DataFrame(np.random.randn(10, 4), columns=['a', 'b', 'c', 'd'])
df1.plot.area(colormap = 'Greens_r',alpha = 0.5,ax = axes[0])
df2.plot.area(stacked=False,colormap = 'Set2',alpha = 0.5,ax = axes[1])
# 使用Series.plot.area()和DataFrame.plot.area()創(chuàng)建面積圖
# stacked:是否堆疊,默認(rèn)情況下吆录,區(qū)域圖被堆疊
# 為了產(chǎn)生堆積面積圖窑滞,每列必須是正值或全部負(fù)值!
# 當(dāng)數(shù)據(jù)有NaN時(shí)候恢筝,自動(dòng)填充0哀卫,所以圖標(biāo)簽需要清洗掉缺失值
圖片.png
# 填圖
fig,axes = plt.subplots(2,1,figsize = (8,6))
x = np.linspace(0, 1, 500)
y1 = np.sin(4 * np.pi * x) * np.exp(-5 * x)
y2 = -np.sin(4 * np.pi * x) * np.exp(-5 * x)
axes[0].fill(x, y1, 'r',alpha=0.5,label='y1')
axes[0].fill(x, y2, 'g',alpha=0.5,label='y2')
# 對(duì)函數(shù)與坐標(biāo)軸之間的區(qū)域進(jìn)行填充,使用fill函數(shù)
# 也可寫(xiě)成:plt.fill(x, y1, 'r',x, y2, 'g',alpha=0.5)
x = np.linspace(0, 5 * np.pi, 1000)
y1 = np.sin(x)
y2 = np.sin(2 * x)
axes[1].fill_between(x, y1, y2, color ='b',alpha=0.5,label='area')
# 填充兩個(gè)函數(shù)之間的區(qū)域撬槽,使用fill_between函數(shù)
for i in range(2):
axes[i].legend()
axes[i].grid()
# 添加圖例此改、格網(wǎng)
圖片.png
# 餅圖 plt.pie()
# plt.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None,
# radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, hold=None, data=None)
s = pd.Series(3 * np.random.rand(4), index=['a', 'b', 'c', 'd'], name='series')
plt.axis('equal') # 保證長(zhǎng)寬相等
plt.pie(s,
explode = [0.1,0.2,0,0],
labels = s.index,
colors=['r', 'g', 'b', 'c'],
autopct='%.2f%%',
pctdistance=0.6,
labeldistance = 1.2,
shadow = True,
startangle=0,
radius=1.5,
frame=False)
print(s)
# 第一個(gè)參數(shù):數(shù)據(jù)
# explode:指定每部分的偏移量
# labels:標(biāo)簽
# colors:顏色
# autopct:餅圖上的數(shù)據(jù)標(biāo)簽顯示方式
# pctdistance:每個(gè)餅切片的中心和通過(guò)autopct生成的文本開(kāi)始之間的比例
# labeldistance:被畫(huà)餅標(biāo)記的直徑,默認(rèn)值:1.1
# shadow:陰影
# startangle:開(kāi)始角度
# radius:半徑
# frame:圖框
# counterclock:指定指針?lè)较颍槙r(shí)針或者逆時(shí)針
圖片.png