42.Matplotlib条形图

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>

猜你喜欢

转载自blog.csdn.net/xzy53719/article/details/82799913