import pandas as pd data=pd.read_excel('生活费开销.xlsx')
我想在jupyter上直接显示pygal的图形,需要创建html的基础模板,你们可以拿去直接用:
import pygal #设置pygal与jupyter notebook交互 from IPython.display import display, HTML base_html = """
{rendered_chart}
"""
下面进入主题:
1.pygal绘制折线图(主题:DefaultStyle)
from pygal.style import * people=data['人员'].unique() label=data['月份'].unique() line_chart = pygal.Line(style=DefaultStyle) line_chart.title = '520寝室2020年生活费花销情况' line_chart.x_labels = label for i in people: line_chart.add(i, data[data.人员==i]['花销'].values.tolist()) HTML(base_html.format(rendered_chart=line_chart.render(is_unicode=True)))#图片渲染
2.pygal绘制柱状图(主题:DarkStyle、NeonStyle)
绘制竖状柱状图
from pygal.style import * people=data['人员'].unique() label=data['月份'].unique() line_chart = pygal.Bar(style=DarkStyle) line_chart.title = '520寝室2020年生活费花销情况' line_chart.x_labels = label for i in people: line_chart.add(i, data[data.人员==i]['花销'].values.tolist()) HTML(base_html.format(rendered_chart=line_chart.render(is_unicode=True)))#图片渲染
绘制横状柱状图
from pygal.style import * people=data['人员'].unique() label=data['月份'].unique() line_chart = pygal.HorizontalBar(style=NeonStyle) line_chart.title = '520寝室2020年生活费花销情况' line_chart.x_labels = label for i in people: line_chart.add(i, data[data.人员==i]['花销'].values.tolist()) HTML(base_html.format(rendered_chart=line_chart.render(is_unicode=True)))#图片渲染
3.pygal绘制饼图(主题:DarkSolarizedStyle)
普通饼状图
from pygal.style import * people=data['人员'].unique() label=data['月份'].unique() line_chart = pygal.Pie(style=DarkSolarizedStyle) line_chart.title = '520寝室2020年1月生活费花销情况' line_chart.x_labels = label for i in people: line_chart.add(i, data[(data.人员==i)&(data.月份=='1月')]['花销'].values.tolist()) HTML(base_html.format(rendered_chart=line_chart.render(is_unicode=True)))#图片渲染
圆环图
from pygal.style import * people=data['人员'].unique() label=data['月份'].unique() pie_chart = pygal.Pie(inner_radius=0.45,style=LightSolarizedStyle) pie_chart.title = '520寝室2020年1月生活费花销情况' for i in people: pie_chart.add(i, data[(data.人员==i)&(data.月份=='1月')]['花销'].values.tolist()[0]) HTML(base_html.format(rendered_chart=pie_chart.render(is_unicode=True)))#图片渲染
4.pygal绘制雷达图(主题:LightStyle)
from pygal.style import * people=data['人员'].unique() label=data['月份'].unique() radar_chart = pygal.Radar(style=LightStyle) radar_chart.title = '520寝室2020年生活费花销情况' radar_chart.x_labels = label for i in people: radar_chart.add(i, data[data.人员==i]['花销'].values.tolist()) HTML(base_html.format(rendered_chart=radar_chart.render(is_unicode=True)))#图片渲染
5.pygal绘制箱形图(主题:CleanStyle)
from pygal.style import * people=data['人员'].unique() label=data['月份'].unique() box_plot = pygal.Box(style=CleanStyle) box_plot.title = '520寝室2020年生活费花销情况' for i in people: box_plot.add(i, data[data.人员==i]['花销'].values.tolist()) HTML(base_html.format(rendered_chart=box_plot.render(is_unicode=True)))#图片渲染
6.pygal绘制散点图(主题:RedBlueStyle)
from pygal.style import * people=data['人员'].unique() label=data['月份'].unique() dot_chart = pygal.Dot(x_label_rotation=30,style=RedBlueStyle) dot_chart.title = '520寝室2020年生活费花销情况' dot_chart.x_labels=label for i in people: dot_chart.add(i, data[data.人员==i]['花销'].values.tolist()) HTML(base_html.format(rendered_chart=dot_chart.render(is_unicode=True)))#图片渲染
7.pygal绘制漏斗图(主题:DarkColorizedStyle)
from pygal.style import * people=data['人员'].unique() label=data['月份'].unique() funnel_chart = pygal.Funnel(style=DarkColorizedStyle) funnel_chart.title = '520寝室2020年生活费花销情况' funnel_chart.x_labels=label for i in people: funnel_chart.add(i, data[data.人员==i]['花销'].values.tolist()) HTML(base_html.format(rendered_chart=funnel_chart.render(is_unicode=True)))#图片渲染
8.pygal绘制仪表盘图(主题:LightColorizedStyle)
from pygal.style import * people=data['人员'].unique() label=data['月份'].unique() gauge_chart = pygal.Gauge(human_readable=True,style=LightColorizedStyle) gauge_chart.title = '520寝室2020年1月生活费花销情况' gauge_chart.range = [0, 5000] for i in people: gauge_chart.add(i, data[(data.人员==i)&(data.月份=='1月')]['花销'].values.tolist()) HTML(base_html.format(rendered_chart=gauge_chart.render(is_unicode=True)))#图片渲染