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()
参考:读芯术课程学习