42.Matplotlib條形圖
阿新 • • 發佈:2018-12-11
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
np.random.seed(0) x = np.arange(5) y = np.random.randint(-5,5,5) #ncols 列數量,2列子圖 fig,ax = plt.subplots(ncols=2) #縱向條形圖 v_bars = ax[0].bar(x,y,color='r') #橫向條形圖 h_bars = ax[1].barh(x,y,color='b') #加入一條直線 ax[0].axhline(0,color='grey',linewidth=2) ax[1].axvline(0,color='grey',linewidth=2)
<matplotlib.lines.Line2D at 0x854ecc0>
fig,ax=plt.subplots()
v_var = ax.bar(x,y,color='lightblue')
#不同區域用不同顏色
for bar,height in zip(v_var,y):
if height<0:
bar.set(edgecolor='darkred',color='green',linewidth=3)
#影象填充,面積圖 x = np.random.randn(100).cumsum() y = np.linspace(0,10,100) fig,ax = plt.subplots() ax.fill_between(x,y,color='lightblue')
<matplotlib.collections.PolyCollection at 0x54523c8>
x = np.linspace(0,10,200)
y1 = 2*x + 1
y2 = 3*x + 2
y_mean = 0.5 * x * np.cos(2 * x) + 2.5 * x +1.1
fig,ax = plt.subplots()
ax.plot(x,y1)
ax.plot(x,y2)
ax.plot(x,y_mean)
ax.fill_between(x,y1,y2,color='lightblue')
<matplotlib.collections.PolyCollection at 0x4d04f60>