文章目录
前言
一、首先引入相关Python库
二、实例Demo
1.1 官方Demo
1.2 将实际数据应用于官方Demo
总结
前言
Matplotlib,官方提供的饼图Demo,功能比较简单,在实际应用过程中,往往会有许多个性化的绘制需求,在这里跟大家一起了解一下饼图(pie chart)的一些特色功能的实现。
一、首先引入相关Python库
from matplotlib import font_manager as fm
import matplotlib as mpl
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('ggplot')
二、实例Demo
1.1 官方Demo
代码如下(示例):
import matplotlib.pyplot as plt
# Pie chart, where the slices will be ordered and plotted counter-clockwise
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice(i.e. 'Hogs')
fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',shadow=True, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle
plt.savefig('Demo_official.jpg')
plt.show()
运行结果如下图:
1.2 将实际数据应用于官方Demo
代码如下(示例):
# 将实际数据应用于官方Demo
# 原始数据
shapes = ['Cross', 'Cone', 'Egg', 'Teardrop', 'Chevron', 'Diamond', 'Cylinder', 'Rectangle',
'Flash', 'Cigar', 'Changing', 'Formation', 'Oval', 'Disk',
'Sphere', 'Fireball', 'Triangle', 'Circle', 'Light']
values = [287, 383, 842, 866, 1187, 1405, 1495, 1620, 1717, 2313, 2378, 3070, 4332, 5841, 6482, 7785,
9358, 9818, 20254]
s = pd.Series(values, index=shapes)
from matplotlib import font_manager as fm
import matplotlib as mpl
# Pie chart, where the slices will be ordered and plotted counter-clockwisse:
labels = s.index
sizes = s.values
explode = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) # only "explode" the 1st slice
fig1, ax1 = plt.subplots()
patches, texts, autotexts = ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.0f%%',
shadow=False, startangle=170)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle
plt.savefig('Demo_project.jpg')
plt.show()
运行结果如下图:
上图的一些问题:
1. 颜色比较生硬
2. 部分文字拥挤在一起,绘图显示不齐整
1.3 一些改善措施
§ 重新设置字体大小
§ 设置自选颜色
§ 设置图例
§ 将某些类别突出显示
1.3.1 重新设置字体大小
代码如下:
from matplotlib import font_manager as fm
import matplotlib as mpl
# Pie chart, where the slices will be ordered and plotted counter-clockwisse:
labels = s.index
sizes = s.values
explode = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) # only "explode" the 1st slice
fig1, ax1 = plt.subplots()
patches, texts, autotexts = ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.0f%%',
shadow=False, startangle=170)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle
# 重新设置字体大小
proptease = fm.FontProperties()
proptease.set_size('xx-small')
# font size include: 'xx-small', x-small, 'small', 'medium', 'large', 'x-large', xx-large or number, e.g. '12'
plt.setp(autotexts, fontproperties=proptease)
plt.setp(texts, fontproperties=proptease)
plt.savefig('Demo_project_set_font.jpg')
plt.show()
运行结果如下图:
1.3.2 设置显示颜色,Method1:
示例代码如下:
# 设置显示颜色,Method1
from matplotlib import font_manager as fm
import matplotlib as mpl
# Pie chart,where the slices will be ordered and plotted counter-clockwisse
labels = s.index
sizes = s.values
explode = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) # only "explode" the 1st sli
fig1, ax1 = plt.subplots(figsize=(6,6))
a = np.random.rand(1, 19)
color_vals = list(a[0])
my_norm = mpl.colors.Normalize(-1, 1) # 将颜色数据的范围设置为[0, 1]
my_cmap = mpl.cm.get_cmap('rainbow', len(color_vals)) # 可选择合适的colormap, 如:'rainbow'
patches, texts, autotexts = ax1.pie(sizes, explode=explode, labels=labels,
autopct='%1.0f%%',
shadow=False, startangle=170, colors=my_cmap(my_norm(color_vals)))
ax1.axis('equal')
# 重新设置字体大小
proptease = fm.FontProperties()
proptease.set_size('xx-small')
plt.setp(autotexts, fontproperties=proptease)
plt.setp(texts, fontproperties=proptease)
plt.savefig('Demo_project_set_color_1.jpg')
plt.show()
运行结果如下图:
上面这种方法设置颜色时,但类别比较多时,部分颜色的填充会重复。有时候,可能想设置成连续的颜色,可用另一种方法实现。
1.3.3设置显示颜色, Method2:
示例代码如下:
# 设置颜色方法Method2
from matplotlib import font_manager as fm
from matplotlib import cm
labels = s.index
sizes = s.values
fig1, ax1 = plt.subplots(figsize=(6,6))
colors = cm.rainbow(np.arange(len(sizes))/len(sizes)) # colormaps: Paired, autumn, rainbow, gray, spring, Darks
patches, texts, autotexts = ax1.pie(sizes, explode=explode, labels=labels,
autopct='%1.0f%%',
shadow=False, startangle=170, colors=colors)
ax1.axis('equal')
ax1.set_title('Shapes -------------------------------', loc='left')
# 重新设置字体大小
proptease = fm.FontProperties()
proptease.set_size('xx-small')
plt.setp(autotexts, fontproperties=proptease)
plt.setp(texts, fontproperties=proptease)
plt.savefig('Demo_project_set_color_2.jpg')
plt.show()
运行结果如下图:
从上图可以看出,颜色显示是连续的,实现了我们想要的效果。
1.3.4 设置图例
示例代码如下:
# 设置图例(legend)
from matplotlib import font_manager as fm
from matplotlib import cm
labels = s.index
sizes = s.values
fig1, ax1 = plt.subplots(figsize=(6,6))
colors = cm.rainbow(np.arange(len(sizes))/len(sizes))
patches, texts, autotexts = ax1.pie(sizes, explode=explode, labels=labels,
autopct='%1.0f%%',
shadow=False, startangle=170, colors=colors)
ax1.axis('equal')
# 重新设置字体大小
proptease = fm.FontProperties()
proptease.set_size('xx-small')
plt.setp(autotexts, fontproperties=proptease)
plt.setp(texts, fontproperties=proptease)
ax1.legend(labels, loc=2)
plt.savefig('Demo_project_set_legend_error.jpg')
plt.show()
运行结果如下图:
从上图可看出,当类别较多时,图例(legend)的位置摆放显示有重叠,显示有些问题,需要进行调整 。
1.3.5 重新设置图例(legend)
示例代码如下:
# 重新设置图例(legend)
from matplotlib import font_manager as fm
from matplotlib import cm
labels = s.index
sizes = s.values
fig, axes = plt.subplots(figsize=(10, 5), ncols=2) # 设置绘图区域大小
ax1, ax2 = axes.ravel()
colors = cm.rainbow(np.arange(len(sizes))/len(sizes))
patches, texts, autotexts = ax1.pie(sizes, explode=explode, labels=labels,
autopct='%1.0f%%',
shadow=False, startangle=170, colors=colors)
ax1.axis('equal')
# 重新设置字体大小
proptease = fm.FontProperties()
proptease.set_size('xx-small')
plt.setp(autotexts, fontproperties=proptease)
plt.setp(texts, fontproperties=proptease)
ax1.set_title('Shapes', loc='center')
# ax2只显示图例(legend)
ax2.axis('off')
ax2.legend(patches, labels, loc='center left')
plt.tight_layout()
plt.savefig('Demo_project_set_legend_good.jpg')
plt.show()
运行结果如下图:
1.3.6 将某些类别突出显示
§ 将某些类别突出显示
§ 控制label的显示位置
§ 控制百分比的显示位置
§ 控制突出位置的大小
示例代码如下:
# 1.3.6 将某些类别突出显示
from matplotlib import font_manager as fm
from matplotlib import cm
labels = s.index
sizes = s.values
explode = (0.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.2, 0, 0, 0, 0.1, 0) # only "explode" the 1st sli
fig, axes = plt.subplots(figsize=(10, 5), ncols=2) # 设置绘图区域大小
ax1, ax2 = axes.ravel()
colors = cm.rainbow(np.arange(len(sizes))/len(sizes)) # colormaps:Paired, autumn, rainbow, gray, spring, Darks
patches, texts, autotexts = ax1.pie(sizes, explode=explode, labels=labels,
autopct='%1.0f%%',
shadow=False, startangle=170, colors=colors, labeldistance=1.2,
pctdistance=1.03, radius=0.4)
# labeldistance:控制labels显示的位置
# pctdistance:控制百分比显示的位置
# radius:控制切片突出的距离
ax1.axis('equal')
# 重新设置字体大小
proptease = fm.FontProperties()
proptease.set_size('xx-small')
plt.setp(autotexts, fontproperties=proptease)
plt.setp(texts, fontproperties=proptease)
ax1.set_title('Shapes', loc='center')
# ax2只显示图例(legend)
ax2.axis('off')
ax2.legend(patches, labels, loc='center left')
plt.tight_layout()
plt.savefig('Demo_project_final.jpg')
plt.show()
运行结果如下图:
总结
本文的案例取自Python数据之道,以上练习的内容,仅仅简单介绍了matplotlib的使用,而matplotlib功能模块提供了大量使用方法,大家可以多多练习实践。