matplotlib 画图

折线图绘制

import matplotlib.pyplot as plt
first_twelve = unrate[0:12]

plt.plot(first_twelve['DATE'],first_twelve['VALUE'], c = 'red', label = '折线1')  //折线图的x y,c为设置颜色,label为加标签

plt.plot(unrate[12:23]['DATE'],unrate[12:23]['VALUE']) // 在同一个图中添加多个线


plt.xticks(rotation =45) //x坐标倾斜度数
plt.xlabel('Month')  // x坐标的标签
plt.ylabel('Unemploymehnt Rate') //y坐标标签

plt.title('XXXXXXX') // 折线图的标题

plt.legend(loc ='upper left' )  //显示折线的标签 upper left 为在左边显示 bset为

plt.show()  //展示图


子图添加

ax1 = plt.figure(figsize=(10,7)).add_subplot(2,2,1)  // 建立figure空间,figsize设置图的大小,add_subplot()为添加子图
ax2 = plt.figure(figsize=(3,3)).add_subplot(2,2,2)  
ax3 = plt.figure(figsize=(3,3)).add_subplot(2,2,4)//添加子图位置

ax1.plot(unrate.head()['DATE'],unrate.head()['VALUE'] //给子图添加x,y 值
ax2.plot(np.random.randint(1,5,5),np.arange(5))

plt.show()


柱状图

fig, ax = plt.subplots()  //返回值ax得到的轴对图进行操作

ax.bar([2,5,6,8,10],[2,4,5,1,2],0.3) /参数1为柱的位置,2为高度,3为宽度

散点图

fig, ax = plt.subplots()

ax.scatter(x,y)

盒图

fig , ax =plt.subplots()

ax.boxplot()





猜你喜欢

转载自blog.csdn.net/weixin_42309501/article/details/80476680
今日推荐