Chartify一个基于Bokeh却完爆它的简单的Pytho
1说明:
1。1Chartify库:是由Spotify开源了一个Python库,可让人轻松创建图表。
1。2而Spotify是全球最大的正版流媒体音乐服务平台之一,最新开源的作品Chartify帮助数据科学家用Python创建图表。
1。3Chartify是一个基于Bokeh却完爆Bokeh的数据可视化静态作图,适合嵌入论文、PPT等报告中,操作简单,自动打开浏览器,自动生成html,还可以保存为png格式的图片。
1。4Chartify具有以下特性:
一致的输入数据格式:花费更少的时间来转换数据,所有绘图功能都使用一致的输入数据格式。
智能样式:创建漂亮的图表,基本无需你自定义
简单的API:API尽可能直观且易于学习。
灵活性:Chartify基于Bokeh创建,因此如果用户可以随时使用Bokeh的API。
2准备:
2。1官网:https:github。comspotifychartify
2。2安装:pip3installchartify本机安装sudopip3。8installchartify太慢了sudopip3。8installihttps:mirrors。aliyun。compypisimplechartify超快
2。3环境:
华为笔记本电脑、深度deepinlinux操作系统、谷歌浏览器、python3。8和微软vscode编辑器。
3柱状图:
3。1代码:importchartifydatachartify。examples。exampledata()自带数据集groupedbardata(data。groupby(〔country,fruit〕)〔〔quantity〕〕。sum()。resetindex())xaxistypecategorical,代表x轴的样式,分类样式chchartify。Chart(xaxistypecategorical)主标题和副标题ch。settitle(barchart)ch。setsubtitle(1柱状图)x和y坐标轴的label标签名ch。axes。setxaxislabel(水果和国家)ch。axes。setyaxislabel(质量)ch。plot。bar(dataframegroupedbardata,categoricalcolumns〔fruit,country〕,numericcolumnquantity,colorcolumnfruit)浏览器展示并自动在同目录下保存为同名的html文件ch。show()默认展示html
3。2图:
4stackedbar:
4。1代码:importchartifydatachartify。examples。exampledata()quantitybyfruitandcountry(data。groupby(〔fruit,country〕)〔quantity〕。sum()。resetindex())chchartify。Chart(blanklabelsTrue,xaxistypecategorical)ch。settitle(Stackedbarchart)ch。setsubtitle(Stackcolumnsbyacategoricalfactor。)ch。plot。barstacked(dataframequantitybyfruitandcountry,categoricalcolumns〔fruit〕,numericcolumnquantity,stackcolumncountry,normalizeFalse)ch。show()
4。2图:
5Scatter:
5。1代码:importchartifydatachartify。examples。exampledata()chchartify。Chart(blanklabelsTrue,xaxistypedatetime)ch。plot。scatter(dataframedata,xcolumndate,ycolumnunitprice,sizecolumnquantity,colorcolumnfruit,分组的颜色定义)ch。settitle(Scatterplot)ch。setsubtitle(Optionalcolorcolumnargumentforgroupingbycolor。)ch。show()
5。2图:
6line:
6。1代码:注意注释掉1组数据的line线图importchartifydatachartify。examples。exampledata()一组数据的线图pricebydate(data。groupby(date)〔totalprice〕。sum()。resetindex()Movedatefromindextocolumn)4组数据需要这个pricebydateandcountry(data。groupby(〔date,fruit〕)〔totalprice〕。sum()。resetindex())chchartify。Chart(blanklabelsTrue,xaxistypedatetime)ch。settitle(Linecharts)ch。setsubtitle(Plottwonumericvaluesconnectedbyanorderedline。)ch。plot。line(dataframepricebydate。sortvalues(date),一组数据dataframepricebydateandcountry。sortvalues(date),四组数据xcolumndate,ycolumntotalprice,colorcolumnfruit,一组数据不需要这个;4组数据需要这个)ch。show()
6。2图:
7hexbin:
7。1代码:importchartifydatachartify。examples。exampledata()chchartify。Chart(blanklabelsTrue,xaxistypedensity,yaxistypedensity)ch。settitle(Hexbin)ch。plot。hexbin(dataframedata,xvaluescolumnunitprice,yvaluescolumnquantity,size。2,orientationpointytop)ch。show()
7。2图:
8radar:
8。1代码:importchartifydatachartify。examples。exampledata()totalbyfruitandcountrydata。groupby(〔fruit,country〕)〔quantity〕。sum()。resetindex()chchartify。RadarChart(True)注意这个true,存在,那么x和y坐标轴label就不显示默认,很棒图标的主标题和副标题ch。settitle(RadarAreaChart)ch。setsubtitle(Eachvertexplottedcounterclockwisestartingfromtop)ch。plot。text(totalbyfruitandcountry。groupby(country)〔quantity〕。max()。resetindex(),quantity,textcolumncountry,textaligncenter)ch。plot。area(totalbyfruitandcountry,quantity,colorcolumnfruit)ch。axes。hideyaxis()隐藏y坐标轴ch。axes。hidexaxis()隐藏x坐标轴ch。setlegendlocation(outsidebottom)图例在底部ch。show()
8。2图:
9lollipop:
9。1代码:importchartifydatachartify。examples。exampledata()quantitybyfruitandcountry(data。groupby(〔fruit,country〕)〔quantity〕。sum()。resetindex())chchartify。Chart(blanklabelsTrue,yaxistypecategorical)ch。settitle(Lollipopchart)ch。setsubtitle(Sameoptionsasbarplot)ch。plot。lollipop(dataframequantitybyfruitandcountry,categoricalcolumns〔country,fruit〕,numericcolumnquantity,colorcolumncountry)ch。show()
9。2图:
10stackedarea:
10。1代码:importpandasaspdimportchartifydatachartify。examples。exampledata()totalquantitybymonthandfruit(data。groupby(〔data〔date〕pd。offsets。MonthBegin(1),fruit〕)〔quantity〕。sum()。resetindex()。rename(columns{date:month})。sortvalues(month))chchartify。Chart(blanklabelsTrue,xaxistypedatetime)ch。settitle(Stackedarea)ch。setsubtitle(Representchangesindistribution。)ch。plot。area(dataframetotalquantitybymonthandfruit,xcolumnmonth,ycolumnquantity,colorcolumnfruit,stackedTrue)ch。show()
10。2图:
自己整理并分享出来
喜欢的人,请点赞、关注、转发、评论和收藏。
比官方文档还仔细。