大家可能已经习惯了用matplotlib和seaborn来制作不同的图表,但是今天要介绍一个非常酷的python手绘风格的可视化包:cutecharts。
这个包可以用来生成以下几种看起来像手绘的图表,在某些场景下效果可能更好。这些可爱的图表还具有交互性和动态性。每当鼠标在图表上悬停时,数字就会显示出来。而要创建这种图表,你只需要几行python代码。
目前,该库支持五种图表--条形图、线形图、饼图、雷达图和散点图。它还支持图表的组合。
在开始绘制可爱的图表之前,我们需要安装 cutechart 库。
$ pip install cutecharts
安装好后我们来尝试画下条形图和线图。首先创建下数据,以某个城市的温度数据为例。
#import library and dataimport cutecharts.charts as ctcdf=pd.dataframe({ ‘x’:[‘sun.’,’mon.’,’tue.’,’wed.’,’thu.’,’fri.’,’sat.’], ‘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(‘toronto temperature’,width=’500px’,height=’400px’)chart.set_options( labels=list(df[‘x’]), x_label='days', y_label='temperature (celsius)' , colors=[‘#1eafae’ for i in range(len(df))] )chart.add_series('this week',list(df[‘y’]))chart.render_notebook()
效果:
在这个条形图中,所有的条形图都有相同的颜色。如果你想自定义每个条形图的颜色,你只需要更改一行代码。
chart = ctc.bar(‘title’,width=’500px’,height=’400px’)chart.set_options( labels=list(df[‘x’]), x_label=”days”, y_label=”temperature (celsius)” , colors=[‘#fff1c9’,’#f7b7a3',’#ea5f89',’#9b3192',’#57167e’,’#47b39c’,’#00529b’] )chart.add_series(“this week”,list(df[‘y’]))chart.render_notebook()
2
线图
如果想观察时间序列数据的变动差异,线图无疑更直观。
代码:
chart = ctc.line(“toronto temperature”,width=’500px’,height=’400px’)chart.set_options( labels=list(df[‘x’]), x_label=”days”, y_label=”temperature (celsius)” )chart.add_series(“this week”, list(df[‘y’])) chart.add_series(“last week”, list(df[‘z’]))chart.render_notebook()
还有一个特别的功能:
当你把鼠标悬停在图表上时,图表会自动显示带有数字的标签,而且还画了一条虚线,这样本周和上周的气温差异就更加直观了。
3
雷达图
要将线型图改为雷达图,你只需要将图表类型改为ctc.radar。
代码:
chart = ctc.radar(‘toronto temperature’,width=’700px’,height=’600px’)chart.set_options( labels=list(df[‘x’]), is_show_legend=true, #by default, it is true. you can turn it off. legend_pos=’upright’ #location of the legend )chart.add_series(‘this week’,list(df[‘y’]))chart.add_series(“last week”,list(df[‘z’]))chart.render_notebook()
效果:
4
饼图
我们需要另一个数据集来制作饼图和甜甜圈图。
创建数据集:
df=pd.dataframe({‘x’:[‘asia’, ‘africa’, ‘europe’, ‘north america’, ‘south america’, ‘australia’], ‘y’:[59.69, 16, 9.94, 7.79, 5.68, 0.54]})
这个数据集包含了大洲名称和人口占比。
chart = ctc.pie(‘% of population by continent’,width=’500px’,height=’400px’)chart.set_options( labels=list(df[‘x’]), inner_radius=0 )chart.add_series(list(df[‘y’])) chart.render_notebook()
效果:
而且把饼图变成甜甜圈图也很容易。你只需要改变inner_radius的参数。
代码:
df=pd.dataframe({‘x’:[‘asia’, ‘africa’, ‘europe’, ‘north america’, ‘south america’, ‘australia’], ‘y’:[59.69, 16, 9.94, 7.79, 5.68, 0.54]})chart = ctc.pie(‘% of population by continent’,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
散点图
为了绘制散点图,我将创建一个新的数据集。这次我们用到的是温度和冰淇淋销量数据。
数据集:
temperature = [14.2,16.4,11.9,15.2,18.5,22.1,19.4,25.1,23.4,18.1,22.6,17.2]sales = [215,325,185,332,406,522,412,614,544,421,445,408]
散点图代码:
chart = ctc.scatter(‘ice cream sales vs temperature’,width=’500px’,height=’600px’)chart.set_options( x_label=”temperature (celcius)”, y_label=”icecream sales” , colors=[‘#1eafae’], is_show_line = false, dot_size=1)chart.add_series(“temperature”, [(z[0], z[1]) for z in zip(temperature, sales)])chart.render_notebook()
6
组合图
如果你想把多个图表组合在一起,那么代码也不复杂。
chart1 = ctc.line(“toronto temperature”,width=’500px’,height=’400px’)chart1.set_options( labels=list(df[‘x’]), x_label=”days”, y_label=”temperature (celsius)” )chart1.add_series(“this week”, list(df[‘y’])) chart1.add_series(“last week”, list(df[‘z’]))chart2 = ctc.bar(‘toronto temperature’,width=’500px’,height=’400px’)chart2.set_options( labels=list(df[‘x’]), x_label=”days”, y_label=”temperature (celsius)” , colors=[‘#1eafae’ for i in range(len(df))] )chart2.add_series(“this week”,list(df[‘y’]))chart2.add_series(“last week”,list(df[‘z’]))page = page()page.add(chart1, chart2)page.render_notebook()
cutecharts这个包非常简单易用,如果你也喜欢这个风格的图表,就赶快试一下。
搭载Radeon显卡的台式机处理器
小米CC9 Pro渲染图及新配色发布,摄像头性能主打一亿像素
iReader和Kindle哪个最好
基于航空RFID行李处理应用方案的介绍
苹果订购5G iphone的PCB板三家PCB台企获单,有望今年发售
一个非常酷的Python手绘风格的可视化包:cutecharts
目前新能源汽车的各方面还存在着缺陷
集成电路国家队武汉新芯三管齐下 超前研发高精尖项目
西安临潼区“智慧秦岭”管控指挥中心建成运行,实现无人机空中管护
国能准能集团研发矿山数字孪生系统 填补国内采矿行业空白
使用GaN器件实现汽车电气化
三星Galaxy Note9,带指纹传感器,将于8月9日发布
K100A万能空调遥控器操作步骤使用说明书
步进电机制作小型木工车床
湖南湘江地平线信息技术有限公司揭牌仪式在湖南省长沙市正式举行
如何释放700M黄金频段的红利?
如何提高永磁同步电机制造自动化系统的能源效率
iQOO Pro新品发布会三天倒计时,5G强悍再次加速
P1.5LED软模组技术参数及性能概述
家电维修案例