matplotlib进行绘图——条形图

参考刘顺祥 数据分析1480

分为七个步骤:

1、导入模块

2、设置绘图风格

3、导入数据

4、设置图框的大小

5、绘图

6、添加轴标签和标题

7、显示图形

#导入模块
import matplotlib.pyplot as plt


# 设置绘图风格
# print (plt.style.available)
plt.style.use('fast')
plt.rcParams['font.sans-serif'] =['Microsoft YaHei']
# plt.rcParams['axes.unicode_minus'] = False

# 构建数据
GDP = [12406.8,13908.57,9386.87,9143.64]

# 设置图框的大小
fig = plt.figure(num =1, figsize=(10, 3), facecolor = 'y')

# 绘图
plt.bar(range(4), GDP, width=0.2, align = 'center',color='b', alpha = 0.8, label=["GDP"])
plt.ylim([0,15000])

# 添加轴标签、标题、刻度标签
plt.ylabel('GDP')
plt.title('四个直辖市GDP大比拼')
plt.xticks(range(4),['北京市','上海市','-天津市','重庆市'])
for x,y in enumerate(GDP):
    plt.text(x,y+300,str(round(y,1)), horizontalalignment='center')
    
    
# 显示图形
fig.autofmt_xdate(rotation = 45)
plt.legend( title = "图例", loc = 'best' )
plt.show()

bingo:

猜你喜欢

转载自blog.csdn.net/Scarlett_Guan/article/details/85247843