Matplotlib绘制柱状图和饼状图

1.柱状图

程序示例:

'''1.简单示例'''
import matplotlib.pyplot as plt

num_list = [1.5, 0.6, 7.8, 6]

plt.bar(range(len(num_list)), num_list)
plt.show()

'''2.添加颜色'''
import matplotlib.pyplot as plt

num_list = [1.5, 0.6, 7.8, 6]

plt.bar(range(len(num_list)), num_list, color='rgbc')   # 每个柱子用不同颜色
plt.show()

'''3.更改x轴的显示'''
import matplotlib.pyplot as plt
name_list = ['Monday', 'Tuseday', 'Friday', 'Sunday']
num_list = [1.5, 0.6, 7.8, 6]

plt.bar(range(len(num_list)), num_list, color='rgb', tick_label=name_list)   # 每个柱子用不同颜色
plt.show()

'''4.堆叠柱状图,添加图例'''
import matplotlib.pyplot as plt
name_list = ['Monday', 'Tuseday', 'Friday', 'Sunday']
num_list = [1.5, 0.6, 7.8, 6]
num_list2 = [1, 2, 2, 1]
plt.bar(range(len(num_list)), num_list, tick_label=name_list,label='boys')
plt.bar(range(len(num_list)), num_list, tick_label=name_list, bottom=num_list, label='girls')
plt.legend(loc='best')
plt.show()

'''5.横向柱状图'''
import matplotlib.pyplot as plt
name_list = ['Monday', 'Tuseday', 'Friday', 'Sunday']
num_list = [1.5, 0.6, 7.8, 6]
plt.barh(range(len(num_list)), num_list, tick_label=name_list)
plt.show()

'''6.并排柱状图,添加图例'''
import matplotlib.pyplot as plt
name_list = ['Monday', 'Tuseday', 'Friday', 'Sunday']
num_list = [1.5, 0.6, 7.8, 6]
num_list2 = [1, 2, 2, 1]

x = list(range(len(num_list)))
total_width = 0.8
n = 2
width = total_width / n
plt.bar(x, num_list, width=width, label='boys')
for i in range(len(x)):
    x[i] = x[i] + width
plt.bar(x, num_list2, width=width, tick_label=name_list, label='girls')
plt.legend(loc='best')
plt.show()

 

2.饼状图

 

程序示例

'''1.简单饼状图示例'''
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']    #正常显示中文

labels = 'A','B','C','D'
sizes = [10,20,30,40]

plt.pie(sizes, labels = labels)
plt.title('简单饼状图')
plt.show()

'''2.设置饼到中心的位置'''
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']    #正常显示中文

labels = 'A','B','C','D'
sizes = [10,20,30,40]
explode = (0,0,0.1,0)   # 第三块饼离开中心位置

plt.pie(sizes, labels = labels, explode=explode)
plt.title('简单饼状图')
plt.show()

'''3.添加颜色属性'''
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']    #正常显示中文

labels = 'A','B','C','D'
sizes = [10,20,30,40]
explode = (0,0,0.1,0)   # 第三块饼离开中心位置
colors = ['r', 'g', 'b', 'c']
plt.pie(sizes, labels = labels, explode=explode, colors=colors)
plt.title('简单饼状图')
plt.show()

'''4.在饼中显示比例autopct'''
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']    #正常显示中文

labels = 'A','B','C','D'
sizes = [10,20,30,40]
explode = (0,0,0.1,0)   # 第三块饼离开中心位置
colors = ['r', 'g', 'b', 'c']
plt.pie(sizes, labels = labels, explode=explode, colors=colors, autopct='%1.2f%%')
plt.title('简单饼状图')
plt.show()

'''5.调整饼的比例'''
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']    #正常显示中文

labels = 'A','B','C','D'
sizes1 = [10,20,30,40]
sizes2 = [0.1,0.2,0.3,0.4]
sizes3 = [0.1,0.2,0.3,0.7]
sizes4 = [0.1,0.2,0.3,0.2]
explode = (0,0,0.1,0)   # 第三块饼离开中心位置
colors = ['r', 'g', 'b', 'c']
plt.subplot(2,2,1)
plt.pie(sizes1, labels = labels, explode=explode, colors=colors, autopct='%1.2f%%')
plt.title('简单饼状图>1')
plt.subplot(2,2,2)
plt.pie(sizes2, labels = labels, explode=explode, colors=colors, autopct='%1.2f%%')
plt.title('简单饼状图=1')
plt.subplot(2,2,3)
plt.pie(sizes3, labels = labels, explode=explode, colors=colors, autopct='%1.2f%%')
plt.title('简单饼状图>1')
plt.subplot(2,2,4)
plt.pie(sizes4, labels = labels, explode=explode, colors=colors, autopct='%1.2f%%')
plt.title('简单饼状图<1')
plt.show()

'''6.添加图例'''
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']    #正常显示中文

labels = 'A','B','C','D'
sizes = [10,20,30,40]
explode = (0,0,0.1,0)   # 第三块饼离开中心位置
colors = ['r', 'g', 'b', 'c']
plt.pie(sizes, labels = labels, explode=explode, colors=colors, autopct='%1.2f%%')
plt.title('简单饼状图')
plt.legend(loc='best',fontsize=8,borderaxespad=0.3)
plt.show()

 

参考:读芯术课程学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值