Package: matchart
MatChartMatChart is a convenient one-line wrapper aroundmatplotlibplotting library.Usagefrommatchartimportplotplot([x1,y1],[y2],[x3,y3],y4,...,# common parameterskind='plot',show:bool=True,block:Optional[bool]=None,context:bool=False,clear_on_error:bool=True,# plotter explict parameterslabel:ClippedArguments=None,color:CycledArguments=None,marker:ClippedArguments=None,linestyle:ClippedArguments=None,linewidth:ClippedArguments=None,markersize:ClippedArguments=None,# figure and axes parameterslegend:Optional[bool]=None,legend_kwargs:Optional[Dict[str,Any]]=None,title:Optional[str]=None,title_kwargs:Optional[Dict[str,Any]]=None,xlabel:Optional[str]=None,xlabel_kwargs:Optional[Dict[str,Any]]=None,ylabel:Optional[str]=None,ylabel_kwargs:Optional[Dict[str,Any]]=None,limit:Union[Tuple[Any,Any,Any,Any],bool]=True,xticks:Optional[Union[Iterable,Dict[str,Any],bool]]=None,yticks:Optional[Union[Iterable,Dict[str,Any],bool]]=None,ticks:Optional[Dict[str,Union[Iterable,Dict[str,Any],bool]]]=None,figsize:Tuple[float,float]=(10,8),dpi:float=100,subplots_kwargs:Optional[Dict[str,Any]]=None,grid:Optional[bool]=False,grid_kwargs:Optional[Dict[str,Any]]=None,theme='seaborn-v0_8-deep',# plotter rest parameters**plotter_kwargs)->Tuple[Figure,Axes,List[Artist]]kind- pyplotfunction name, e.g.plotorscatter.show- whether to show plot or just to draw it.block- whether to block running code when showing plot or just display windows and run next lines. By default, detected from environment.context- delay showing plot by using context manager (see below).clear_on_error- whether to clean up on any error.label,color,marker,linestyle,linewidth,markersizeand restplotter_kwargs- plotter parameters. Can differ per kind. Seecommon parametersforkind='plot'. Can belistortupleto define per-dataset values. Values can also beNoneor even clipped to skip definition for particular dataset.legend- setTrue,FalseorNoneto autodetect.legend_kwargs-additional parameters.title- plot's title.title_kwargs-additional parameters.xlabel- horizontal axis label.xlabel_kwargs-additional parameters.ylabel- vertical axis label.ylabel_kwargs-additional parameters.limit- setTrueto autodetect 2D plot's borders orFalseto use default matplot's behaviour. Set to(left, right, bottom, top)to usecustom borders.ticks,xticks,yticks- setcorresponding ticks. Can beFalseto disable ticks. Can be dictionary of kwargs. Can be array-like value to define ticks locations. Argumentxticksdefines ticks for X axis,yticks- for Y axis. Alternatively argumenttickscan be defined as dictionary with keys'x'or'y'containing same values asxticksandyticksarguments. Argumentxticksmust be used solely.figsize- plot's size as(width, height)in inches. By default, is overriden with10x8.dpi- plot's resolution in dots-per-inch.subplots_kwargs-additional plot's parametersandFigure's parameters.grid- plot'sgrid visibilitywithgrid_kwargsas additional parameters.theme- plot'sstyle. By default is overriden withseaborn-v0_8-deep.ExamplesJust plot somethingFirstly prepare data:importnumpyasnpx1=np.arange(21)y1=x1**2y2=x1**1.9x3=5,15y3=300,100y4=x1**2.1Then plot data:frommatchartimportplotplot([x1,y1],[y2],[x3,y3],y4,label=['$x^{2}$','$x^{1.9}$','Points'],xlabel='X',ylabel='Y',title='Power of $x$ comparison.',grid=True,color=[None,'green','red','gray'],marker=['o',None,'*'],linestyle=[None,'--'],linewidth=[None,3,0],markersize=20,fillstyle='top')Migrate from matplotlib simpleLet's takeFiveThirtyEight style sheet example:importmatplotlib.pyplotaspltimportnumpyasnpplt.style.use('fivethirtyeight')x=np.linspace(0,10)np.random.seed(19680801)# Fixing random state for reproducibilityfig,ax=plt.subplots()ax.plot(x,np.sin(x)+x+np.random.randn(50))ax.plot(x,np.sin(x)+0.5*x+np.random.randn(50))ax.plot(x,np.sin(x)+2*x+np.random.randn(50))ax.plot(x,np.sin(x)-0.5*x+np.random.randn(50))ax.plot(x,np.sin(x)-2*x+np.random.randn(50))ax.plot(x,np.sin(x)+np.random.randn(50))ax.set_title("'fivethirtyeight' style sheet")plt.show()and rewrite it with MatChart:frommatchartimportplotimportnumpyasnpx=np.linspace(0,10)np.random.seed(19680801)# Fixing random state for reproducibilityplot([x,np.sin(x)+x+np.random.randn(50)],[x,np.sin(x)+0.5*x+np.random.randn(50)],[x,np.sin(x)+2*x+np.random.randn(50)],[x,np.sin(x)-0.5*x+np.random.randn(50)],[x,np.sin(x)-2*x+np.random.randn(50)],[x,np.sin(x)+np.random.randn(50)],title="'fivethirtyeight' style sheet",theme='fivethirtyeight')Note that default figure size differs.Migrate from matplotlib stackplotLet's takestackplots example:importmatplotlib.pyplotasplt# data from United Nations World Population Prospects (Revision 2019)# https://population.un.org/wpp/, license: CC BY 3.0 IGOyear=[1950,1960,1970,1980,1990,2000,2010,2018]population_by_continent={'africa':[228,284,365,477,631,814,1044,1275],'americas':[340,425,519,619,727,840,943,1006],'asia':[1394,1686,2120,2625,3202,3714,4169,4560],'europe':[220,253,276,295,310,303,294,293],'oceania':[12,15,19,22,26,31,36,39],}fig,ax=plt.subplots()ax.stackplot(year,population_by_continent.values(),labels=population_by_continent.keys(),alpha=0.8)ax.legend(loc='upper left')ax.set_title('World population')ax.set_xlabel('Year')ax.set_ylabel('Number of people (millions)')plt.show()and rewrite it with MatChart:frommatchartimportplot# data from United Nations World Population Prospects (Revision 2019)# https://population.un.org/wpp/, license: CC BY 3.0 IGOyear=[1950,1960,1970,1980,1990,2000,2010,2018]population_by_continent={'africa':[228,284,365,477,631,814,1044,1275],'americas':[340,425,519,619,727,840,943,1006],'asia':[1394,1686,2120,2625,3202,3714,4169,4560],'europe':[220,253,276,295,310,303,294,293],'oceania':[12,15,19,22,26,31,36,39],}plot([year,population_by_continent.values()],kind='stackplot',labels=population_by_continent.keys(),alpha=0.8,legend=True,legend_kwargs=dict(loc='upper left'),limit=False,title='World population',xlabel='Year',ylabel='Number of people (millions)')Note that default figure size and theme differ.Customization of plotsLet's take FiveThirtyEight style sheet example above and customize it before showing.Imagine now we want to remove X and Y axes.Straightforward solution (not bad)To do the trick we can:only draw plot withshow=False;customize figure withmatplotlibstuff;show plot withmatplotlib.pyplot.show().frommatchartimportplotimportnumpyasnpfrommatplotlibimportpyplotaspltx=np.linspace(0,10)np.random.seed(19680801)# Fixing random state for reproducibilityfig,ax,arts=plot([x,np.sin(x)+x+np.random.randn(50)],[x,np.sin(x)+0.5*x+np.random.randn(50)],[x,np.sin(x)+2*x+np.random.randn(50)],[x,np.sin(x)-0.5*x+np.random.randn(50)],[x,np.sin(x)-2*x+np.random.randn(50)],[x,np.sin(x)+np.random.randn(50)],title="'fivethirtyeight' style sheet",theme='fivethirtyeight',show=False)ax.xaxis.set_visible(False)ax.yaxis.set_visible(False)plt.show()Clever solution (better)To solve such kind of problems there iscontext managersupport:draw plot withcontext=True;customize figure withmatplotlibstuff within context manager.frommatchartimportplotimportnumpyasnpx=np.linspace(0,10)np.random.seed(19680801)# Fixing random state for reproducibilitywithplot([x,np.sin(x)+x+np.random.randn(50)],[x,np.sin(x)+0.5*x+np.random.randn(50)],[x,np.sin(x)+2*x+np.random.randn(50)],[x,np.sin(x)-0.5*x+np.random.randn(50)],[x,np.sin(x)-2*x+np.random.randn(50)],[x,np.sin(x)+np.random.randn(50)],title="'fivethirtyeight' style sheet",theme='fivethirtyeight',context=True)asresults:ax=results[1]ax.xaxis.set_visible(False)ax.yaxis.set_visible(False)The best solutionSpecially for lazy people there is special alias to be used withincontext manager:draw plot withmatchart.cplot();customize figure withmatplotlibstuff within context manager.frommatchartimportcplotimportnumpyasnpx=np.linspace(0,10)np.random.seed(19680801)# Fixing random state for reproducibilitywithcplot([x,np.sin(x)+x+np.random.randn(50)],[x,np.sin(x)+0.5*x+np.random.randn(50)],[x,np.sin(x)+2*x+np.random.randn(50)],[x,np.sin(x)-0.5*x+np.random.randn(50)],[x,np.sin(x)-2*x+np.random.randn(50)],[x,np.sin(x)+np.random.randn(50)],title="'fivethirtyeight' style sheet",theme='fivethirtyeight')asplotter:plotter.axis.xaxis.set_visible(False)plotter.axis.yaxis.set_visible(False)Now you can customize plots just adding literally one letter.Changelog1.1.5:Add getters to context plotter.1.1.4:Add ticks control parameters.Add automatic cleanup on any error (parameterclear_on_error).1.1.3:Addcontextparameter and aliasmatchart.cplot(...)to delay plot showing within context manager.Addblockparameter ofmatplotlib's show function.Small enhancements and bugs fixes.