使用盲文点在终端绘制。
plotille的Python项目详细描述
绘图仪
在终端使用盲文点绘制散点图和直方图,几乎没有依赖关系。用颜色绘制或生成复杂的图形-类似于matplotlib的一个非常小的兄弟。或者用画布自己绘制点和线。
安装:
pip install plotille
与其他库类似:
- 类似于termplot,但使用盲文(更细的点)、从左到右的直方图和用于绘制函数的线性插值。
- 就像termgraph(不在pypi上),但风格截然不同。
- ^ terminalplot,但有盲文、x/y轴、直方图、线性插值。
TimeSeries Plotting的基本支持随3.2版提供:对于任何X
或Y
值,也可以添加datetime.datetime
、pendulum.datetime
或numpy.datetime64
值。根据x_limits
和y_limits
的差异生成标签。
文档
In[1]:importplotilleIn[2]:importnumpyasnpIn[3]:X=np.sort(np.random.normal(size=1000))
图
要构造绘图,建议使用Figure
:
In[12]:plotille.Figure?Initsignature:plotille.Figure()Docstring:Figureclasstocomposemultipleplots.WithinaFigureyoucaneasilycomposemanyplots,assignlabelstoplotsanddefinethepropertiesoftheunderlyingCanvas.Possiblepropertiesthatcanbedefinedare:width,height:intDefinethenumberofcharactersinX/Ydirectionwhichareusedforplotting.x_limits:floatDefinetheXlimitsofthereferencecoordinatesystem,thatwillbeplottered.y_limits:floatDefinetheYlimitsofthereferencecoordinatesystem,thatwillbeplottered.color_mode:strDefinetheusedcolormode.See`plotille.color()`.with_colors:boolDefine,whethertousecolorsatall.background:multipleDefinethebackgroundcolor.x_label,y_label:strDefinetheX/Yaxislabel.
基本上,您可以创建一个Figure
,定义属性并添加绘图。使用show()
函数,Figure
使用新画布生成绘图:
In[13]fig=plotille.Figure()In[14]fig.width=60In[15]fig.height=30In[16]fig.set_x_limits(min_=-3,max_=3)In[17]fig.set_y_limits(min_=-1,max_=1)In[18]fig.color_mode='byte'In[19]fig.plot([-0.5,1],[-1,1],lc=25,label='First line')In[20]fig.scatter(X,np.sin(X),lc=100,label='sin')In[21]fig.plot(X,(X+2)**2,lc=200,label='square')In[22]print(fig.show(legend=True))
可用的绘图功能有:
# create a plot with linear interpolation between pointsFigure.plot(self,X,Y,lc=None,interp='linear',label=None)# create a scatter plot with no interpolation between pointsFigure.scatter(self,X,Y,lc=None,label=None)# create a histogram over XFigure.histogram(self,X,bins=160,lc=None)
其他有趣的功能有:
# remove all plots from the figureFigure.clear(self)# Create a canvas, plot the registered plots and return the string for displaying the plotFigure.show(self,legend=False)
制图:
有一些实用函数可以快速绘制单个绘图。
绘图:
In[4]:plotille.plot?Signature:plt.plot(X,Y,width=80,height=40,X_label='X',Y_label='Y',linesep='\n',interp='linear',x_min=None,x_max=None,y_min=None,y_max=None,lc=None,bg=None,color_mode='names',origin=True,)Docstring:CreateplotwithX,YvaluesandlinearinterpolationbetweenpointsParameters:X:List[float]Xvalues.Y:List[float]Yvalues.XandYmusthavethesamenumberofentries.width:intThenumberofcharactersforthewidth(columns)ofthecanvas.hight:intThenumberofcharactersforthehight(rows)ofthecanvas.X_label:strLabelforX-axis.Y_label:strLabelforY-axis.max8characters.linesep:strTherequestedlineseperator.default:os.linesepinterp:Optional[str]Specifyinterpolation;valuesNone,'linear'x_min,x_max:floatLimitsforthedisplayedXvalues.y_min,y_max:floatLimitsforthedisplayedYvalues.lc:multipleGivethelinecolor.bg:multipleGivethebackgroundcolor.color_mode:strSpecifycolorinputmode;'names'(default),'byte'or'rgb'seeplotille.color.__docs__origin:boolWhethertoprinttheorigin.default:TrueReturns:str:plotover`X`,`Y`.In[5]:print(plotille.plot(X,np.sin(X),height=30,width=60))
散布:
In[6]:plotille.scatter?Signature:plt.scatter(X,Y,width=80,height=40,X_label='X',Y_label='Y',linesep='\n',x_min=None,x_max=None,y_min=None,y_max=None,lc=None,bg=None,color_mode='names',origin=True,)Docstring:CreatescatterplotwithX,YvaluesBasicallyplottingwithoutinterpolation:`plot(X,Y,...,interp=None)`Parameters:X:List[float]Xvalues.Y:List[float]Yvalues.XandYmusthavethesamenumberofentries.width:intThenumberofcharactersforthewidth(columns)ofthecanvas.hight:intThenumberofcharactersforthehight(rows)ofthecanvas.X_label:strLabelforX-axis.Y_label:strLabelforY-axis.max8characters.linesep:strTherequestedlineseperator.default:os.linesepx_min,x_max:floatLimitsforthedisplayedXvalues.y_min,y_max:floatLimitsforthedisplayedYvalues.lc:multipleGivethelinecolor.bg:multipleGivethebackgroundcolor.color_mode:strSpecifycolorinputmode;'names'(default),'byte'or'rgb'seeplotille.color.__docs__origin:boolWhethertoprinttheorigin.default:TrueReturns:str:scatterplotover`X`,`Y`.In[7]:print(plotille.scatter(X,np.sin(X),height=30,width=60))
历史:
灵感来自crappyhist。
In[8]:plotille.hist?Signature:plotille.hist(X,bins=40,width=80,log_scale=False,linesep='\n',lc=None,bg=None,color_mode='names')Docstring:Createhistogramover`X`fromlefttorightThevaluesontheleftarethecenterofthebucket,i.e.`(bin[i]+bin[i+1])/2`.Thevaluesontherightarethetotalcountsofthisbucket.Parameters:X:List[float]Theitemstocountover.bins:intThenumberofbinstoputXentriesin(rows).width:intThenumberofcharactersforthewidth(columns).log_scale:boolScalethehistogramwith`log`function.linesep:strTherequestedlineseperator.default:os.lineseplc:multipleGivethelinecolor.bg:multipleGivethebackgroundcolor.color_mode:strSpecifycolorinputmode;'names'(default),'byte'or'rgb'seeplotille.color.__docs__Returns:str:histogramover`X`fromlefttoright.In[9]:print(plotille.hist(np.random.normal(size=10000)))
直方图:
还有另一个更“常用”的直方图函数可用:
In[10]:plotille.histogram?Signature:plotille.histogram(X,bins=160,width=80,height=40,X_label='X',Y_label='Counts',linesep='\n',x_min=None,x_max=None,y_min=None,y_max=None,lc=None,bg=None,color_mode='names')Docstring:Createhistogramover`X`Incontrastto`hist`,thisisthemore`usual`histogramfrombottomtoup.TheX-axisrepresentsthevaluesin`X`andtheY-axisisthecorrespondingfrequency.Parameters:X:List[float]Theitemstocountover.bins:intThenumberofbinstoputXentriesin(columns).height:intThenumberofcharactersfortheheight(rows).X_label:strLabelforX-axis.Y_label:strLabelforY-axis.max8characters.linesep:strTherequestedlineseperator.default:os.linesepx_min,x_max:floatLimitsforthedisplayedXvalues.y_min,y_max:floatLimitsforthedisplayedYvalues.lc:multipleGivethelinecolor.bg:multipleGivethebackgroundcolor.color_mode:strSpecifycolorinputmode;'names'(default),'byte'or'rgb'seeplotille.color.__docs__Returns:str:histogramover`X`.In[11]:print(plotille.histogram(np.random.normal(size=10000)))
画布:
底层绘图区域被建模为Canvas
类:
In[12]:plotille.Canvas?Initsignature:plotille.Canvas(width,height,xmin=0,ymin=0,xmax=1,ymax=1,background=None,color_mode='names')Docstring:AcanvasobjectforplottingbrailledotsACanvasobjecthasa`width`x`height`characterslargecanvas,inwhichitcanplotindivitualbraillepoint,linesoutofbraillepoints,rectangles,...Sinceafullbraillecharacterhas2x4dots(⣿),thecanvashas`width`*2,`height`*4dotstoplotintointotal.Itmaintainstwocoordinatesystems:areferencesystemwiththelimits(xmin,ymin)inthelowerleftcornerto(xmax,ymax)intheupperrightcorneristransformedintothecanvasdiscrete,i.e.dots,coordinatesystem(0,0)to(`width`*2,`height`*4).ItdoessotransparentlytoclientsoftheCanvas,i.e.allplottingfunctionsonlyacceptcoordinatesinthereferencesystem.Ifthecoordinatesareoutsidethereferencesystem,theyarenotplotted.Initdocstring:InitiateaCanvasobjectParameters:width:intThenumberofcharactersforthewidth(columns)ofthecanvas.hight:intThenumberofcharactersforthehight(rows)ofthecanvas.xmin,ymin:floatLowerleftcornerofreferencesystem.xmax,ymax:floatUpperrightcornerofreferencesystem.background:multipleBackgroundcolorofthecanvas.color_mode:strThecolor-modeforallcolorsofthiscanvas;either'names'(default)'rgb'or'byte'.See`plotille.color()`.Returns:Canvasobject
最有趣的功能是:
点:
Signature:plotille.Canvas.point(self,x,y,set_=True,color=None)Docstring:Putapointintothecanvasat(x,y)[referencecoordinatesystem]Parameters:x:floatx-coordinateonreferencesystem.y:floaty-coordinateonreferencesystem.set_:boolWhethertoplotorremovethepoint.color:multipleColorofthepoint.
行:
In[14]:plotille.Canvas.line?Signature:plotille.Canvas.line(self,x0,y0,x1,y1,set_=True,color=None)Docstring:Plotlinebetweenpoint(x0,y0)and(x1,y1)[referencecoordinatesystem].Parameters:x0,y0:floatPoint0x1,y1:floatPoint1set_:boolWhethertoplotorremovetheline.color:multipleColoroftheline.
rect:
In[15]:Canvas.rect?Signature:plotille.Canvas.rect(self,xmin,ymin,xmax,ymax,set_=True,color=None)Docstring:Plotrectanglewithbbox(xmin,ymin)and(xmax,ymax)[referencecoordinatesystem].Parameters:xmin,ymin:floatLowerleftcornerofrectangle.xmax,ymax:floatUpperrightcornerofrectangle.set_:boolWhethertoplotorremovetherect.color:multipleColoroftherect.
绘图:
In[16]:Canvas.plot?Signature:plotille.Canvas.plot(self,x_axis=False,y_axis=False,y_label='Y',x_label='X',linesep='\n')Docstring:Transformcanvasinto`print`-ablestringParameters:x_axis:boolAddaX-axisatthebottom.y_axis:boolAddaY-axistotheleft.y_label:strLabelforY-axis.max8characters.x_label:strLabelforX-axis.linesep:strTherequestedlineseperator.default:os.linesepReturns:unicode:Thecancasasastring.
例如,您可以使用它在终端中绘制房屋:
In[17]:c=Canvas(width=40,height=20)In[18]:c.rect(0.1,0.1,0.6,0.6)In[19]:c.line(0.1,0.1,0.6,0.6)In[20]:c.line(0.1,0.6,0.6,0.1)In[21]:c.line(0.1,0.6,0.35,0.8)In[22]:c.line(0.35,0.8,0.6,0.6)In[23]:print(c.plot())