大家可能习惯了用Matplotlib和seaborn制作不同的图表,但今天要介绍的是一个非常酷的Python手绘风格可视化包:cutecharts。使用该包可以生成如下类似手绘图的图表,在某些场景下效果可能会更好。这些可爱的图表也是交互式和动态的。每当鼠标悬停在图表上时,就会显示数字。而要创建这样的图表,您只需要几行Python代码。目前,该库支持五种类型的图表——条形图、折线图、饼图、雷达图和散点图。它还支持图表的组合。在我们开始画可爱的图表之前,我们需要安装可爱的图表库。$pipinstallcucharts安装完成后,我们来尝试绘制条形图和折线图。先创建下一个数据,以某城市的温度数据为例。#importlibraryanddataimportcucharts.chartsasctcdf=pd.DataFrame({'x':['周日','周一','周二','周三','周四','周五','周六'],'y':[14,15,17,20,22.3,23.7,24.8],'z':[16,16.4,23.6,24.5,19.9,13.6,13.4]})1.条形图代码:chart=ctc.Bar('TorontoTemperature',width='500px',height='400px')chart.set_options(labels=list(df['x']),x_label='Days',y_label='温度(摄氏度)',colors=['#1EAFAE'foriinrange(len(df))])chart.add_series('Thisweek',list(df['y']))chart.render_notebook()效果:在这个条形图中,所有的条都有相同的颜色。如果你想自定义每个条形的颜色,你只需要改一行代码。chart=ctc.Bar('title',width='500px',height='400px')chart.set_options(labels=list(df['x']),x_label=”天”,y_label=”温度(摄氏度)",colors=['#FFF1C9','#F7B7A3','#EA5F89','#9B3192','#57167E','#47B39C','#00529B'])chart.add_series("本周",list(df['y']))chart.render_notebook()2.折线图如果想观察时间序列数据的变化,折线图无疑更直观。代码:chart=ctc.Line("TorontoTemperature",width='500px',height='400px')chart.set_options(labels=list(df['x']),x_label="Days",y_label="Temperature(Celsius)")chart.add_series("ThisWeek",list(df['y']))chart.add_series("LastWeek",list(df['z']))chart.render_notebook()还有一个特别的功能:当你将鼠标悬停在图形上时,图表会自动显示带数字的标签,同时也会画一条虚线,这样本周和上周的温差更直观。3.雷达图将折线图改为雷达图,只需要ch将图表类型更改为ctc.Radar。代码:chart=ctc.Radar('TorontoTemperature',width='700px',height='600px')chart.set_options(labels=list(df['x']),is_show_legend=True,#bydefault,itistrue.Youcanturnitoff.legend_pos='upRight'#locationofthelegend)chart.add_series('Thisweek',list(df['y']))chart.add_series("上周",list(df['z']))chart.render_notebook()效果:4.饼图我们需要另外一个数据集来制作饼图和圆环图。创建数据集:df=pd.DataFrame({'x':['亚洲','非洲','欧洲','北美','南美洲','澳大利亚'],'y':[59.69,16,9.94,7.79,5.68,0.54]})此数据集包含大洲名称和人口百分比。chart=ctc.Pie('%ofpopulationbycontinent',width='500px',height='400px')chart.set_options(labels=list(df['x']),inner_radius=0)chart.add_series(列表(df['y']))chart.render_notebook()的效果:把饼图变成圆环图也很容易。您只需要更改inner_radius参数。代码:df=pd.DataFrame({'x':['亚洲','非洲','欧洲','北美','南美','澳大利亚'],'y':[59.69,16,9.94,7.79,5.68,0.54]})chart=ctc.Pie('%ofpopulationbycontinent',width='500px',height='400px')chart.set_options(labels=list(df['x']),inner_radius=0.6)chart.add_series(list(df['y']))chart.render_notebook()5.散点图要绘制散点图,我将创建一个新数据集。这次我们使用温度和冰淇淋销售数据。数据:温度=[14.2,16.4,11.9,15.2,18.5,22.1,199.4,25.1,23.4,23.4,18.1,1,22.6,17.2]width='500px',height='600px')chart.set_options(x_label="Temperature(Celcius)",y_label="IcecreamSales",colors=['#1EAFAE'],is_show_line=False,dot_size=1)chart.add_series("Temperature",[(z[0],z[1])forzinzip(Temperature,Sales)])chart.render_notebook()6.合并图表如果要合并多个图表,代码并不复杂。chart1=ctc.Line("TorontoTemperature",width='500px',height='400px')chart1.set_options(labels=list(df['x']),x_label="天",y_label="温度(摄氏度))")chart1.add_series("ThisWeek",list(df['y']))chart1.add_series("LastWeek",list(df['z']))chart2=ctc.Bar('TorontoTemperature',宽度='500px',height='400px')chart2.set_options(labels=list(df['x']),x_label=”Days”,y_label=”Temperature(Celsius)”,colors=['#1EAFAE'foriinrange(len(df))])chart2.add_series("本周",list(df['y']))chart2.add_series("上周",list(df['z']))page=Page()page.add(chart1,chart2)page.render_notebook()cutcharts这个包非常简单易用。如果您喜欢这种风格的图表,请尝试一下。
