使用matplotlib库绘制图表最重要的就需要导入numpy模块和pyplot模块 1、绘制直线图`
plt.title("2020080603012")
import numpy as np
import matplotlib.pyplot as plt
data=np.array([3,5,7,9,11])
plt.plot(data)
plt.show()
2、绘制折线图
plt.title("2020080603012")
import numpy as np
import matplotlib.pyplot as plt
x=np.arange(4,19)
y_max=np.array([32,31,35,36,25,38,39,34,28,35,36,31,35,36,34])
plt.plot(x,y_max)
plt.show()
plt.title("2020080603012")
import numpy as np
import matplotlib.pyplot as plt
x=np.arange(4,19)
y_max=np.array([32,31,35,36,25,38,39,34,28,35,36,31,35,36,34])
y_min=np.array([19,15,21,35,26,15,18,24,18,27,27,29,16,25,14])
plt.plot(x,y_max)
plt.plot(x,y_min)
plt.show()
3、绘制柱形图 (1)
plt.title("2020080603012")
import numpy as np
import matplotlib.pyplot as plt
x1=np.arange(5)
y1=np.array([23,15,48,16,25])
bar_width=0.5
plt.bar(x1,y1,tick_label=['a','b','c','d','e'],width=bar_width)
plt.show()
(2)绘制多组柱形图
plt.title("2020080603012")
import numpy as np
import matplotlib.pyplot as plt
x1=np.arange(5)
y1=np.array([23,15,48,16,25])
y3=np.array([12,23,21,25,26])
bar_with=0.5
plt.bar(x1,y1,tick_label=['a','b','c','d','e'],width=bar_width)
plt.bar(x1+bar_width,y3,width=bar_width)
plt.show(
(3)绘制堆积柱形图
plt.title("2020080603012")
import numpy as np
import matplotlib.pyplot as plt
x1=np.arange(5)
y1=np.array([23,15,48,16,25])
y3=np.array([12,23,21,25,26])
plt.bar(x1,y1,tick_label=['a','b','c','d','e'],width=bar_width)
plt.bar(x1,y3,bottom=y2,width=bar_width)
plt.show()
(4)为堆积柱形图添加误差棒
plt.title("2020080603012")
import numpy as np
import matplotlib.pyplot as plt
x1=np.arange(5)
y1=np.array([23,15,48,16,25])
y3=np.array([12,23,21,25,26])
error=[2,1,2.5,2,1.5]
plt.bar(x1,y1,tick_label=['a','b','c','d','e'],width=bar_width)
plt.bar(x1,y3,bottom=y2,width=bar_width,yerr=error)
plt.show()
3、绘制条形图 (1)
plt.title("2020080603012")
import numpy as np
import matplotlib.pyplot as plt
yy=np.arange(5)
xx=np.array([23,15,48,16,25])
bar_height=0.5
plt.barh(yy,xx,tick_label=['a','b','c','d','e'],height=bar_height)
plt.show()
(2)绘制多组条形图
plt.title("2020080603012")
import numpy as np
import matplotlib.pyplot as plt
xx=np.array([23,15,48,16,25])
xx2=np.array([25,12,34,45,57])
yy=np.arange(5)
bar_height=0.5
plt.barh(yy,xx,tick_label=['a','b','c','d','e'],height=bar_height)
plt.barh(yy+bar_height,xx2,height=bar_height)
plt.show()
(3)绘制堆积条形图
plt.title("2020080603012")
import numpy as np
import matplotlib.pyplot as plt
xx2=np.array([25,12,34,45,57])
xx=np.array([23,15,48,16,25])
yy=np.arange(5)
bar_height=0.5
plt.barh(yy,xx,tick_label=['a','b','c','d','e'],height=bar_height)
plt.barh(yy,xx2,left=xx,height=bar_height)
plt.show()
(4)为堆积条形图添加误差棒
plt.title("2020080603012")
xx2=np.array([25,12,34,45,57])
xx=np.array([23,15,48,16,25])
yy=np.arange(5)
error=[2,1,2.5,2,1.5]
plt.barh(yy,xx,tick_label=['a','b','c','d','e'],height=bar_height)
plt.barh(yy,xx2,left=xx,height=bar_height,xerr=error)
plt.show()
|