1、使用Matplotlib生成数据图
Matplotlib是一个非常优秀的Python 2D绘图库,只要给出符合格式的数据,通过Matplotlib可以方便的绘制出折线图、柱状图、散点图等。
1.1、安装Matplotlib包
pip install matplotlib
在成功安装Matplotlib包之后,打开浏览器:http://localhost:8899/ 查看页面,可以在Python安装目录的lib\site-packages下看到Matplotlib包的文档
1.2、Matplotlib数据图入门
1.2.1、折线图举例
举例:折线图(给出对应的X轴、Y轴数据,调用pyplot子模块下的plot()函数)
import matplotlib.pyplot as plt
x_data = ['2016', '2017', '2018', '2019', '2020', '2021', '2022']
y_data = [12000, 11580, 9870, 12950, 13560, 10040, 11490]
plt.plot(x_data, y_data)
plt.show()
结果展示 如果:只传入一个列表,则该列表数据将作为Y轴数据,Matplotlib则自动使用0,1,2…来作为X轴数据
import matplotlib.pyplot as plt
y_data = [12000, 11580, 9870, 12950, 13560, 10040, 11490]
plt.plot(y_data)
plt.show()
结果展示 多条折线
import matplotlib.pyplot as plt
x_data = ['2016', '2017', '2018', '2019', '2020', '2021', '2022']
y_data1 = [12000, 11580, 9870, 12950, 13560, 10040, 11490]
y_data2 = [7500, 8900, 6800, 9200, 8400, 6900, 10200]
plt.plot(x_data, y_data1, x_data, y_data2)
plt.show()
结果展示
1.2.2、图表样式设置
调用plot()函数时可以指定折线的样式:线宽、颜色、样式等。
color:指定颜色
linewidth:指定线宽
linestyle:指定样式
linestyle样式
-:代表实线,默认值
--:代表虚线
::代表点线
-.:代表短线、点相间的虚线
import matplotlib.pyplot as plt
x_data = ['2016', '2017', '2018', '2019', '2020', '2021', '2022']
y_data1 = [12000, 11580, 9870, 12950, 13560, 10040, 11490]
y_data2 = [7500, 8900, 6800, 9200, 8400, 6900, 10200]
plt.plot(x_data, y_data1, color='red', linewidth=2.0, linestyle='--')
plt.plot(x_data, y_data2, color='blue', linewidth=3.0, linestyle='-.')
plt.show()
结果展示
1.3、管理图例
复式折线图需要添加图例,调用legend()函数
import matplotlib.pyplot as plt
x_data = ['2016', '2017', '2018', '2019', '2020', '2021', '2022']
y_data1 = [12000, 11580, 9870, 12950, 13560, 10040, 11490]
y_data2 = [7500, 8900, 6800, 9200, 8400, 6900, 10200]
lin1, = plt.plot(x_data, y_data1, color='red', linewidth=2.0, linestyle=':')
lin2, = plt.plot(x_data, y_data2, color='blue', linewidth=1.5, linestyle='-')
plt.legend(handles=[lin1, lin2], labels=['图例1', '图例2'], loc='lower center')
plt.show()
loc参数设置
'best':自动选择最佳位置
'upper right':将图例放在右上角
'upper left':将图例放在左上角
'lower right':将图例放在右下角
'lower left':将图例放在左下角
'right':将图例放在右边
'center right':将图例放在右边居中的位置
'center left':将图例放在左边居中的位置
'upper center':将图例放在顶部居中的位置
'lower center':将图例放在底部居中的位置
'center ':将图例放在中心
结果展示 中文字符显示为方框或乱码: UserWarning: Glyph 22270 (\N{CJK UNIFIED IDEOGRAPH-56FE}) missing from current font.func(*args) 解决办法一:
import matplotlib
matplotlib.rc("font", family='Microsoft YaHei')
优化后结果 解决办法二:
import matplotlib.font_manager as fm
my_font = fm.FontProperties(fname="C:\Windows\Fonts\msyh.ttf")
legend()函数调用时添加参数prop=my_font
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
my_font = fm.FontProperties(fname="C:\Windows\Fonts\msyh.ttf")
x_data = ['2016', '2017', '2018', '2019', '2020', '2021', '2022']
y_data1 = [12000, 11580, 9870, 12950, 13560, 10040, 11490]
y_data2 = [7500, 8900, 6800, 9200, 8400, 6900, 10200]
lin1, = plt.plot(x_data, y_data1, color='red', linewidth=2.0, linestyle=':')
lin2, = plt.plot(x_data, y_data2, color='blue', linewidth=1.5, linestyle='-')
plt.legend(handles=[lin1, lin2], labels=['图例1', '图例2'], loc='lower center', prop=my_font)
plt.show()
1.4、管理坐标轴
调用xlabel()、ylabel()函数分别设置X轴、Y轴的名称; 调用title()函数设置整个数据图的标题; 调用xticks()、yticks()函数分别改变X轴、Y轴的刻度值;
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc("font", family='Microsoft YaHei')
x_data = ['2016', '2017', '2018', '2019', '2020', '2021', '2022']
y_data1 = [12000, 11580, 9870, 12950, 13560, 10040, 11490]
y_data2 = [7500, 8900, 6800, 9200, 8400, 6900, 10200]
lin1, = plt.plot(x_data, y_data1, color='red', linewidth=2.0, linestyle=':')
lin2, = plt.plot(x_data, y_data2, color='blue', linewidth=1.5, linestyle='-')
plt.legend(handles=[lin1, lin2], labels=['图例1', '图例2'], loc='lower center')
plt.xlabel("年份")
plt.ylabel("金额")
plt.title("折线图测试图表年份-金额")
plt.yticks([9000, 11000, 13000], [r'良好', r'较好', r'优秀'])
plt.show()
结果展示 如果要对X轴、Y轴进行更细致的控制,调用gca()函数来获取轴信息对象
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc("font", family='Microsoft YaHei')
x_data = ['2016', '2017', '2018', '2019', '2020', '2021', '2022']
y_data1 = [12000, 11580, 9870, 12950, 13560, 10040, 11490]
y_data2 = [7500, 8900, 6800, 9200, 8400, 6900, 10200]
lin1, = plt.plot(x_data, y_data1, color='red', linewidth=2.0, linestyle=':')
lin2, = plt.plot(x_data, y_data2, color='blue', linewidth=1.5, linestyle='-')
plt.legend(handles=[lin1, lin2], labels=['图例1', '图例2'], loc='lower center')
plt.xlabel("年份")
plt.ylabel("金额")
plt.title("折线图测试图表年份-金额")
plt.yticks([9000, 11000, 13000], [r'良好', r'较好', r'优秀'])
ax = plt.gca()
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_position(('data', 9000))
plt.show()
结果展示
1.5、管理多个子图
Matplotlib可以生成包含多条折线图之外,还可以在一张数据图上包含多个子图 调用subplot()函数
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
matplotlib.rc("font", family='Microsoft YaHei')
plt.figure()
x_data = np.linspace(-np.pi, np.pi, 64, endpoint=True)
plt.subplot(2, 1, 1)
plt.plot(x_data, np.sin(x_data))
plt.gca().spines['right'].set_color('none')
plt.gca().spines['top'].set_color('none')
plt.gca().spines['bottom'].set_position(('data', 0))
plt.gca().spines['left'].set_position(('data', 0))
plt.title("正弦曲线")
plt.subplot(2, 2, 3)
plt.plot(x_data, np.cos(x_data))
plt.gca().spines['right'].set_color('none')
plt.gca().spines['top'].set_color('none')
plt.gca().spines['bottom'].set_position(('data', 0))
plt.gca().spines['left'].set_position(('data', 0))
plt.title("余弦曲线")
plt.subplot(2, 2, 4)
plt.plot(x_data, np.tan(x_data))
plt.gca().spines['right'].set_color('none')
plt.gca().spines['top'].set_color('none')
plt.gca().spines['bottom'].set_position(('data', 0))
plt.gca().spines['left'].set_position(('data', 0))
plt.title("正切曲线")
plt.show()
结果展示 可通过GridSpec对绘图区域进行分割
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import matplotlib.gridspec as gridspec
matplotlib.rc("font", family='Microsoft YaHei')
plt.figure()
x_data = np.linspace(-np.pi, np.pi, 64, endpoint=True)
gs = gridspec.GridSpec(2, 3)
ax1 = plt.subplot(gs[0, :])
ax2 = plt.subplot(gs[1, 0])
ax3 = plt.subplot(gs[1, 1:3])
ax1.plot(x_data, np.sin(x_data))
ax1.spines['right'].set_color('none')
ax1.spines['top'].set_color('none')
ax1.spines['bottom'].set_position(('data', 0))
ax1.spines['left'].set_position(('data', 0))
ax1.set_title("正弦曲线")
ax2.plot(x_data, np.cos(x_data))
ax2.spines['right'].set_color('none')
ax2.spines['top'].set_color('none')
ax2.spines['bottom'].set_position(('data', 0))
ax2.spines['left'].set_position(('data', 0))
ax2.set_title("余弦曲线")
ax3.plot(x_data, np.tan(x_data))
ax3.spines['right'].set_color('none')
ax3.spines['top'].set_color('none')
ax3.spines['bottom'].set_position(('data', 0))
ax3.spines['bottom'].set_position(('data', 0))
ax3.spines['bottom'].set_position(('data', 0))
ax3.spines['bottom'].set_position(('data', 0))
ax3.spines['bottom'].set_position(('data', 0))
ax3.spines['left'].set_position(('data', 0))
ax3.set_title("正切曲线")
plt.show()
结果展示
2、功能丰富的数据图
2.1、饼图
Matplotlib提供pie()函数绘制饼图
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc("font", family='Microsoft YaHei')
data = [0.16881, 0.14996, 0.07471, 0.06992, 0.04762,
0.03541, 0.02925, 0.02411, 0.02316, 0.01409, 0.36326]
labels = ['JAVA', 'C', 'C++', 'Python', 'Visual Basic.NET', 'C#', 'PHP', 'JavaScript', 'SQL', 'Assembly Language', '其他']
explode = [0, 0, 0.3, 0, 0, 0, 0, 0, 0, 0, 0]
colors = ['red', 'blue', 'yellow', 'purple', 'orange', 'black']
plt.axes(aspect='equal')
plt.xlim(0, 8)
plt.ylim(0, 8)
plt.pie(
x=data,
labels=labels,
explode=explode,
colors=colors,
autopct='%.3f%%',
pctdistance=0.8,
labeldistance=1.15,
startangle=90,
center=(4, 4),
radius=3.8,
counterclock=False,
wedgeprops={'linewidth': 0.5, 'edgecolor': 'green'},
textprops={'fontsize': 10, 'color': 'black'},
frame=1
)
plt.xticks(())
plt.yticks(())
plt.title('编程语言调查情况')
plt.show()
结果展示
2.2、柱状图
Matplotlib提供bar()函数绘制柱状图
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
matplotlib.rc("font", family='Microsoft YaHei')
x_data = ['2016', '2017', '2018', '2019', '2020', '2021', '2022']
y_data1 = [12000, 11580, 9870, 12950, 13560, 10040, 11490]
y_data2 = [7500, 8900, 6800, 9200, 8400, 6900, 10200]
bar_width = 0.3
plt.bar(x=range(len(x_data)), height=y_data1, label='疯狂JAVA讲义', color='red', alpha=0.8, width=bar_width)
plt.bar(x=np.arange(len(x_data))+bar_width, height=y_data2, label='疯狂Python讲义', color='blue', alpha=0.8, width=bar_width)
for x, y in enumerate(y_data1):
plt.text(x, y + 100, '%s' % y, ha='center', va='bottom')
for x, y in enumerate(y_data2):
plt.text(x, y + bar_width, '%s' % y, ha='center', va='top')
plt.title("Java与Python年销量对比")
plt.xlabel('年份')
plt.ylabel('销量')
plt.legend(loc='upper right')
plt.show()
结果展示
2.3、水平柱状图
Matplotlib提供barh()函数绘制柱状图
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
matplotlib.rc("font", family='Microsoft YaHei')
x_data = ['2016', '2017', '2018', '2019', '2020', '2021', '2022']
y_data1 = [12000, 11580, 9870, 12950, 13560, 10040, 11490]
y_data2 = [7500, 8900, 6800, 9200, 8400, 6900, 10200]
bar_width = 0.3
plt.barh(y=range(len(x_data)), width=y_data1, label='疯狂JAVA讲义', color='red', alpha=0.8, height=bar_width)
plt.barh(y=np.arange(len(x_data))+bar_width, width=y_data2, label='疯狂Python讲义', color='blue', alpha=0.8, height=bar_width)
for y, x in enumerate(y_data1):
plt.text(x+5000, y - bar_width/2, '%s' % x, ha='center', va='bottom')
for y, x in enumerate(y_data2):
plt.text(x+5000, y + bar_width/2, '%s' % x, ha='center', va='top')
plt.yticks(np.arange(len(x_data)) + bar_width/2, x_data)
plt.title("Java与Python年销量对比")
plt.xlabel('年份')
plt.ylabel('销量')
plt.legend(loc='upper right')
plt.show()
结果展示
2.4、散点图
Matplotlib提供scatter()函数绘制散点图 scatter()函数参数说明
x:指定X轴数据
y:指定Y轴数据
s:指定散点大小
c:指定散点颜色
alpha:指定散点透明度
linewidths:指定散点边框线的宽度
edgecolors:指定散点边框的颜色
marker:指定散点的图形样式
marker:指定散点的图形样式
'.':点标记
',':像素标记
'o':圆形标记
'v':向下三角形标记
'^':向上三角形标记
'<':向左三角形标记
'>':向右三角形标记
'1':向下三叉标记
'2':向上三叉标记
'3':向左三叉标记
'4':向右三叉标记
's':正方形标记
'p':五边形标记
'*':星形标记
'h':八边形标记
'H':另一种八边形标记
'+':加号标记
'x':x标记
'D':菱形标记
'd':尖菱形标记
'|':竖线标记
'-':横线标记
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
matplotlib.rc("font", family='Microsoft YaHei')
plt.figure()
x_data = np.linspace(-np.pi, np.pi, 64, endpoint=True)
plt.scatter(x_data, np.sin(x_data), c='purple', s=50, alpha=0.8, marker='o', linewidths=0.5, edgecolors=['red', 'blue'])
plt.scatter(x_data[0], np.sin(x_data)[0], c='red', s=150, alpha=1)
plt.scatter(x_data[63], np.sin(x_data)[63], c='black', s=150, alpha=1)
plt.gca().spines['right'].set_color('none')
plt.gca().spines['top'].set_color('none')
plt.gca().spines['bottom'].set_position(('data', 0))
plt.gca().spines['left'].set_position(('data', 0))
plt.title('正弦曲线的散点图')
plt.show()
结果展示
2.4、等高线图
等高线图需要的是三维数据,Matplotlib提供contour()函数绘制等高线,contourf()函数填充颜色 contour()、contourf()函数参数说明
X:指定X轴数据
Y:指定Y轴数据
Z:指定X,Y坐标对应点的高度数据
colors:指定不同高度的等高线颜色
alpha:指定等高线的透明度
cmap:指定等高线的颜色映射
linewidth:指定等高线的宽度
linestyles:指定等高线的样式
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
matplotlib.rc("font", family='Microsoft YaHei')
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X-1)**2 - (Y-1)**2)
Z = (Z1 - Z2)*2
plt.contourf(x, y, Z, 16, alpha=0.75, cmap='rainbow')
C = plt.contour(x, y, Z, 16, colors='black', linewidths=0.5)
plt.clabel(C, inline=True, fontsize=10)
plt.xticks(())
plt.yticks(())
plt.title("等高线图")
plt.xlabel('经度')
plt.ylabel('纬度')
plt.show()
结果展示
2.5、3D图形
3D图形需要的是三维数据,Matplotlib提供Axes3D.plot_surface()函数绘制
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
matplotlib.rc("font", family='Microsoft YaHei')
fig = plt.figure(figsize=(10, 6))
ax = plt.axes(projection="3d")
delta = 0.125
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2)*2
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
ax.set_zlim(-2, 2)
plt.title("3D图形")
plt.show()
结果展示
3、使用Pygal生成数据图
Pygal以面向对象的方式来创建各种数据图
3.1、安装Pygal包
pip install pygal
执行命令后,在浏览器输入:http://localhost:8899/
python -m pydoc -p 8899
3.2、Pygal数据图入门
3.2.1、柱状图举例
大概步骤:
- 创建Pygal数据图对象。Pygal为不同的数据图提供了不同的类:柱状图pygal.Bar类、饼图pygal.Pie类、折线图pygal.Line类;
- 调用数据图对象的.add()方法添加数据;
- 调用Config对象的属性配置数据图;
- 调用数据图对象的render_to_xxx()方法将数据图渲染到指定的输出节点(此处的输出节点可以是PNG图片、SVG文件等);
简单的柱状图举例:
import pygal
x_data = ['2016', '2017', '2018', '2019', '2020', '2021', '2022']
y_data1 = [12000, 11580, 9870, 12950, 13560, 10040, 11490]
y_data2 = [7500, 8900, 6800, 9200, 8400, 6900, 10200]
bar = pygal.Bar()
bar.add('疯狂JAVA讲义', y_data1)
bar.add('疯狂Python讲义', y_data2)
bar.x_labels = x_data
bar.title = "图书销量统计"
bar.x_title = "年份"
bar.y_title = "销量"
bar.render_to_file(r'E:\Users\PycharmProjects\DepressionReservoir\Infos\fk_books.svg')
结果展示
3.2.2、使用render_to_png()方法时报错
bar.render_to_png(r'E:\Users\PycharmProjects\DepressionReservoir\Infos\fk_books.png')
问题原因:缺少cairosvg模块,命令直接安装
pip install cairosvg
3.2.3、render_to_png()方法生成图片中中文乱码
解决方法
from pygal.style import Style
style = Style(font_family="Yahei Consolas Hybrid")
...
bar = pygal.Bar(style=style)
...
3.3、配置Pygal
查阅http://localhost:8899/pygal.config.html 该模块包含BaseConfig、CommonConfig、Config、SeriesConfig等配置类
import pygal
from pygal.style import Style
style = Style(font_family="Yahei Consolas Hybrid")
x_data = ['2016', '2017', '2018', '2019', '2020', '2021', '2022']
y_data1 = [12000, 11580, 9870, 12950, 13560, 10040, 11490]
y_data2 = [7500, 8900, 6800, 9200, 8400, 6900, 10200]
line = pygal.Line(style=style)
line.add('疯狂JAVA讲义', y_data1)
line.add('疯狂Python讲义', y_data2)
line.x_labels = x_data
line.title = "图书销量统计"
line.x_title = "年份"
line.y_title = "销量"
line.x_label_rotation = 45
line.legend_at_bottom = True
line.margin = 35
line.show_y_guides = False
line.show_x_guides = True
line.render_to_file(r'E:\Users\PycharmProjects\DepressionReservoir\Infos\fk_books_line.svg')
line.render_to_png(r'E:\Users\PycharmProjects\DepressionReservoir\Infos\fk_books_line.png')
结果展示
4、Pygal支持的常见数据图
4.1、折线图
pygal.Line类表示折线图
import pygal
from pygal.style import Style
style = Style(font_family="Yahei Consolas Hybrid")
x_data = ['2016', '2017', '2018', '2019', '2020', '2021', '2022']
y_data1 = [12000, 11580, 9870, 12950, 13560, 10040, 11490]
y_data2 = [7500, 8900, 6800, 9200, 8400, 6900, 10200]
line = pygal.Line(style=style)
line.add('疯狂JAVA讲义', y_data1)
line.add('疯狂Python讲义', y_data2)
line.x_labels = x_data
line.title = "图书销量统计"
line.x_title = "年份"
line.y_title = "销量"
line.x_label_rotation = 45
line.legend_at_bottom = True
line.margin = 35
line.show_y_guides = False
line.show_x_guides = True
line.render_to_file(r'E:\Users\PycharmProjects\DepressionReservoir\Infos\fk_books_line.svg')
line.render_to_png(r'E:\Users\PycharmProjects\DepressionReservoir\Infos\fk_books_line.png')
结果展示
4.2、水平柱状图/水平折线图
pygal.HorizontalBar()、pygal.HorizontalLine()
import pygal
from pygal.style import Style
style = Style(font_family="Yahei Consolas Hybrid")
x_data = ['2016', '2017', '2018', '2019', '2020', '2021', '2022']
y_data1 = [12000, 11580, 9870, 12950, 13560, 10040, 11490]
y_data2 = [7500, 8900, 6800, 9200, 8400, 6900, 10200]
horizontal_bar = pygal.HorizontalBar(style=style)
horizontal_bar.add('疯狂JAVA讲义', y_data1)
horizontal_bar.add('疯狂Python讲义', y_data2)
horizontal_bar.x_labels = x_data
horizontal_bar.title = "图书销量统计"
horizontal_bar.x_title = "年份"
horizontal_bar.y_title = "销量"
horizontal_bar.x_label_rotation = 45
horizontal_bar.legend_at_bottom = True
horizontal_bar.margin = 35
horizontal_bar.show_y_guides = False
horizontal_bar.show_x_guides = True
horizontal_bar.render_to_file(r'E:\Users\PycharmProjects\DepressionReservoir\Infos\fk_books_horizontal_bar.svg')
horizontal_bar.render_to_png(r'E:\Users\PycharmProjects\DepressionReservoir\Infos\fk_books_horizontal_bar.png')
结果展示
4.3、叠加柱状图和叠加折线图
pygal.StackedBar()、pygal.StackedLine()
stacked_bar = pygal.StackedBar(style=style)
stacked_line = pygal.StackedLine(style=style)
结果展示
4.4、饼图
pygal.Pie类表示饼图
import pygal
from pygal.style import Style
style = Style(font_family="Yahei Consolas Hybrid")
data = [0.16881, 0.14996, 0.07471, 0.06992, 0.04762,
0.03541, 0.02925, 0.02411, 0.02316, 0.01409, 0.36326]
labels = ['JAVA', 'C', 'C++', 'Python', 'Visual Basic.NET', 'C#', 'PHP', 'JavaScript', 'SQL', 'Assembly Language', '其他']
pie = pygal.Pie(style=style)
for i, per in enumerate(data):
pie.add(labels[i], per)
pie.title = "编程语言调查情况"
pie.lenged_at_bottom = True
pie.inner_radius = 0.4
pie.render_to_file(r'E:\Users\PycharmProjects\DepressionReservoir\Infos\fk_books_pie.svg')
pie.render_to_png(r'E:\Users\PycharmProjects\DepressionReservoir\Infos\fk_books_pie.png')
结果展示
4.5、点图
点图使用点的大小来表示数值的大小,使用pygal.Dot类表示点图
import pygal
from pygal.style import Style
style = Style(font_family="Yahei Consolas Hybrid")
x_data = ['2016', '2017', '2018', '2019', '2020', '2021', '2022']
y_data1 = [12000, 11580, 9870, 12950, 13560, 10040, 11490]
y_data2 = [7500, 8900, 6800, 9200, 8400, 6900, 10200]
dot = pygal.Dot(style=style)
dot.dots_size = 5
dot.add('疯狂JAVA讲义', y_data1)
dot.add('疯狂Python讲义', y_data2)
dot.x_labels = x_data
dot.title = "图书销量统计"
dot.x_title = "年份"
dot.y_title = "销量"
dot.x_label_rotation = 45
dot.legend_at_bottom = True
dot.margin = 35
dot.show_y_guides = False
dot.show_x_guides = True
dot.render_to_file(r'E:\Users\PycharmProjects\DepressionReservoir\Infos\fk_books_dot.svg')
dot.render_to_png(r'E:\Users\PycharmProjects\DepressionReservoir\Infos\fk_books_dot.png')
结果展示
4.6、仪表图
使用pygal.Gauge类表示仪表图
import pygal
from pygal.style import Style
style = Style(font_family="Yahei Consolas Hybrid")
data = [0.16881, 0.14996, 0.07471, 0.06992, 0.04762,
0.03541, 0.02925, 0.02411, 0.02316, 0.01409, 0.36326]
labels = ['JAVA', 'C', 'C++', 'Python', 'Visual Basic.NET', 'C#', 'PHP', 'JavaScript', 'SQL', 'Assembly Language', '其他']
gauge = pygal.Gauge(style=style)
gauge.range = [0, 1]
for i, per in enumerate(data):
gauge.add(labels[i], per)
gauge.title = "编程语言调查情况"
gauge.lenged_at_bottom = True
gauge.inner_radius = 0.4
gauge.render_to_file(r'E:\Users\PycharmProjects\DepressionReservoir\Infos\fk_books_gauge.svg')
gauge.render_to_png(r'E:\Users\PycharmProjects\DepressionReservoir\Infos\fk_books_gauge.png')
结果展示
4.7、雷达图
|