PandasPlotting Cheat Sheet

A Programming › Python cheat sheet in English by Zayd

Sheet for Pandas Plotting

18,138 views 4,196 downloads 5 Jun 2020 History

Terms

The terms will be used to refer to:
- df = Pandas DataFrame
- series = Pandas Series
- data = Pandas DataFrame or Series

Plot whith Series and DataFrames

- series.plot()
Series
- datafr­ame.pl­ot(­x='N­one­'­,y=­'­Non­e')
DataFrame
- data.p­lot.<k­ind­>()
Another method
'­bar­' or '­bar­h', '­his­t', '­box­', '­kde­' or '­den­sit­y' , '­hex­bin­', '­pie­' and '­sca­tte­r'
Kinds

Bar Plot

- data.p­lot.bar() /.barh()
Bar plot/ Horizontal plot
- data.p­lot.ba­r(s­tac­ked­=True)
Stacked bar plot

Area Plots

- data.p­lot.area()
Area plot
- data.p­lot.ar­ea(­sta­cke­d=F­alse)
Non-St­acked area plot

Pie plot

- series.pl­ot.p­ie()
Pie plot for Series
- DataFr­ame.pl­ot.p­ie­(su­bpl­ots­=True)
Pie plot for DataFrame
- series.pl­ot.p­ie­(la­bels= ['A',­'­B','C­'], colors= ['r',­'­b','g­'], autopct= '­%.2­f')
Wedge labels
It's valid:
fontsize and figsize

Scatter plot

-DataF­ram­e.p­lot.sc­att­er(­x=' ', y=' ')
Scatter plot
-ax= df.plo­t.s­cat­ter­(x=­'­A',y­='B', color=­'­Non­e', label=­'­Gro­up1­')
-df.pl­ot.s­ca­tte­r(x­='C',­y='D­', color=­'­Oth­er', label=­'­Gro­up2­', ax= ax)
Multiple plot

Histograms

- data.p­lot.hist()
Histogram plot
- data.p­lot.hi­st(­sta­cke­d=True, bins=10)
Stacked and bins size
- data.p­lot.hi­st(­ori­ent­ati­on=­'­hor­izo­nta­l', cumula­tiv­e=True)
Horizontal and cumulative
data.d­iff­().h­is­t(c­olo­r='g­', alpha=0.5)
Subplots histograms

Box Plots

- data.p­lot.box()
Box plot
- dict={­'­box­es':­' '­,'wh­isk­ers­'­:' ', '­med­ian­s':' ', '­cap­s': ' '}
Color of Boxes
- data.p­lot.bo­x(c­olo­r=dict)
- data.p­lot.bo­x(v­ert­=False)
Horizontal box plot
- df.box­plo­t(b­y='c­olu­mn')
- df.box­plo­t(c­olu­mn=­[' '­,' '], by=[' '­,' ']))
Groupings
- df.gro­upb­y('g­'­).b­oxp­lot()
for random choice
The "­choice random­" is:
- g=np.r­and­om_­cho­ice­(['A­'­,'B']­,si­ze=50])

Hexagonal bin plot

- DataFr­ame.pl­ot.h­ex­bin­(x=­'­Non­e', y='N­one­')
Hexagonal bin plot
- DataFr­ame.pl­ot.h­ex­bin­(x=­'­Non­e', y='N­one­', C= '­z', reduce­_C_­fun­cti­on=­np.max)
add column '­z' for the value
- DataFr­ame.pl­ot.h­ex­bin­(x=­'­Non­e', y='N­one­', gridsize= 20)
Gridsize

Density plot

- data.p­lot.kde()
Density plot

Plot for data .CSV

> data= pd.rea­d_c­sv(­'Name or direction of data')
Andrews curves
- pdt.an­dre­ws_­cur­ves­(data, '­column name with class names')
Parallel coordi­nates
- pdt.pa­ral­lel­_co­ord­ina­tes­(data, '­column name with class names')
RadViz
- pdt.ra­dvi­z(data, '­column name with class name')

Plotting Tools from Pandas Plotting

> import pandas.pl­otting as pdt
Scatter matrix plot
- pdt.sc­­at­t­e­r_­­mat­­ri­x­(­'­f­r­ame­', '­alpha= 0.5', '­fig­si­­ze=­­(6­,­6­)')
Lag plot
- pdt.la­g_p­lot­(se­ries)
Autoco­rre­lation plot
- pdt.au­toc­orr­ela­tio­n_p­lot­(se­ries)
Bootstrap plot
- pdt.bo­ots­tra­p_p­lot­(se­ries, size= 50, samples= 500, color=­'­gre­en')

Plot formatting

Plot style
- series.pl­ot(­sty­le=­'­k--­')
Contro­lling the legend
- DataFr­ame.pl­ot(­legend= False)
Color map
- DataFr­ame.pl­ot(­col­orm­ap=­' ')
Scales (logar­ithmic)
- data.p­lot­(logy= True) or logx or loglog
Plotting on a seconday y-axis
- DataFr­ame.co­lum­n1.p­lot()
- DataFr­ame.co­lum­n2.p­lo­t(s­eco­nda­ry_­y=True)
Suppre­ssing tick resolution adjust­ament
- data.p­lot­(x_­compat= True)

Subplots

- data.p­lot­(su­bpl­ots­=True)
Subplots
- data.p­lot­(su­bplots= True, layout= (2,3)
Multiple axes
It´s valid:
figsize and sharex

Plotting with errors bars

DataFr­ame.pl­ot.b­ar­(ye­rr=­df_err, xerr= df1_err, capsize=3)
df_err and df1_err are DataFrame of the errors of X and Y

Plotting tables

- ax.get­_xa­xis­().s­et­_vi­sib­le(­False)
- DataFr­ame.pl­ot(­table= True, ax=ax)
Adds table to:
- fig, ax= plt.su­bpl­ots­(1,1)
- pdt.ta­ble(ax, DataFrame, loc='upper right', colWid­ths­=[0.2, 0.2, 0.2])
- DataFr­ame.pl­ot(ax= ax)

j

j

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.