python 学习笔记之 matplotlib(饼图)
程序员文章站
2022-03-18 20:45:42
...
matplotlib的一些参数
The following format string characters are accepted to control the line style or marker:
character | description |
---|---|
‘-’ | solid line style |
‘–’ | dashed line style |
‘-.’ | dash-dot line style |
‘:’ | dotted line style |
‘.’ | point marker |
‘,’ | pixel marker |
‘o’ | circle marker |
‘v’ | triangle_down marker |
‘^’ | triangle_up marker |
‘<’ | triangle_left marker |
‘>’ | triangle_right marker |
‘1’ | tri_down marker |
‘2’ | tri_up marker |
‘3’ | tri_left marker |
‘4’ | tri_right marker |
‘s’ | square marker |
‘p’ | petagon marker |
‘*’ | star marker |
‘h’ | hexagon1 marker |
‘H’ | hexagon2 marker |
‘+’ | plus marker |
‘x’ | x marker |
‘D’ | diamond marker |
‘d’ | thin_diamond marker |
’ | ’ |
‘_’ | hline marker |
The following color abbreviations are supported:
character | description |
---|---|
‘b’ | blue |
‘g’ | green |
‘r’ | red |
‘c’ | cyan(青色) |
‘m’ | magenta(紫红色) |
‘y’ | yellow |
‘k’ | black |
‘w’ | white |
In addition, you can specify colors in many weird and wonderful ways, including full names(‘green’), hex string (’#008000’), RGB or RGBA tuples((0,1,0,1)) or grayscale intensities as a string (‘0.8’).
Line styles and colors are combined in a single format string, as in ‘bo’ for blue circles.
matplotlib 实例说明之饼图——pie
一个例子:
import matplotlib.pyplot as plt
labels = 'frogs', 'hogs', 'dogs', 'logs'
sizes = 15, 20, 45, 10
colors = 'yellowgreen', 'gold', 'lightskyblue', 'lightcoral'
explode = 0, 0.1, 0, 0
plt.pie(sizes, explode = explode, labels = labels, colors = colors, autopct = '%1.2f%%', shadow = True, startangle = 50)
# autopct = %1.2f%% ; 小数点后面的数字表示保留小数点几位 ; 分别设置为 %1.2f% 和%1.1f%% 结果分别如1、2图所示
plt.axis('equal')
plt.show()
pie 的参数说明:
参数 | 说明 |
---|---|
x | (每一块)的比例,如果sum(x) > 1会使用sum(x)归一化 |
labels | (每一块)饼图外侧显示的说明文字 |
explode | (每一块)离开中心距离 |
startangle | 起始绘制角度;默认图是从x轴正方向逆时针画起;如设定=90则从y轴正方向画起 |
shadow | 在饼图下面画一个阴影;默认值:False,即不画阴影 |
labeldistance | label标记的绘制位置,相对于半径的比例;默认值为1.1,如<1则绘制在饼图内侧 |
autopct | 控制饼图内百分比设置,可以使用format字符串或者format function; ‘%1.1f’ 指小数点前后位数(没有用空格补齐) |
pctdistance | 类似于labeldistance,指定autopct的位置刻度,默认值为0.6 |
radius | 控制饼图半径,默认值为1 |
counterclock | 指定指针方向;布尔值,可选参数,**默认为:True,即逆时针;**将值改为False即可改为顺时针 |
wedgeprops | 字典类型,可选参数,默认值:None;参数字典传递给 wedge 对象用来画一个饼图。例如:wedgeprops = { ‘linewidth’ : 3 } 设置wedge线宽为3 |
textprops | 设置标签(labels)和比例文字的格式;字典类型,可选参数,默认值为:None;传递给text对象的字典参数 |
center | 浮点类型的列表,可选参数,默认值:(0,0),图标中心位置 |
frame | 布尔类型,可选参数,默认值:False;如果是true,绘制带有表的轴框架 |
rotatelabels | 布尔类型,可选参数,默认为:False;如果为True,旋转每个label到指定的角度 |