Advanced Plotting
Getting Ready
dat2=pd.read_csv("C:\\Users\duan\Desktop\PythonDataProcessingVisualization\meanByClass.txt", sep='\s+')
dat2

Explore a fake gene expression data modified from iris.csv
rpkm=pd.read_csv("C:\\Users\duan\Desktop\PythonDataProcessingVisualization\\fakeExpressionDat.csv")
rpkm

Advanced Line Plot
plt.figure(); dat2.plot(); plt.legend(loc='best')

Get rid of the legend
dat2.plot(legend=False)

Separate the features
dat2.plot(subplots=True, figsize=(6, 6)); plt.legend(loc='best')

Plotting on a Secondary Y-axis
plt.figure()
dat2.WtTypeA.plot(color="b")
dat2.WtTypeB.plot(color="turquoise")
dat2.KOTypeA.plot(color="r")
dat2.KOTypeB.plot(color="pink")
dat2.replicate.plot(secondary_y=True, style='g')

Plot a subset of columns
plt.figure()
dat2.WtTypeA.plot(color="b")
dat2.WtTypeB.plot(color="turquoise")
dat2.KOTypeA.plot(color="r")
dat2.KOTypeB.plot(color="pink")

Selective Plotting on Secondary Y-axis
plt.figure()
dat3=dat2.drop(['replicate'], axis = 1)
ax = dat3.plot(secondary_y=['wtTypeA', 'KOTypeA'])
ax.set_ylabel('TypeB scale')
ax.right_ax.set_ylabel('TypeA scale')

Targeting different subplots by passing an ax argument
fig, axes = plt.subplots(nrows=2, ncols=2)
dat2['WtTypeA'].plot(ax=axes[0,0]); axes[0,0].set_title('WtTypeA')
dat2['KOTypeA'].plot(ax=axes[0,1]); axes[0,1].set_title('KOTypeA')
dat2['WtTypeB'].plot(ax=axes[1,0]); axes[1,0].set_title('WtTypeB')
dat2['KOTypeB'].plot(ax=axes[1,1]); axes[1,1].set_title('KOTypeB')

Adjusting spacing between subplots
fig, axes = plt.subplots(nrows=2, ncols=2)
dat2['WtTypeA'].plot(ax=axes[0,0]); axes[0,0].set_title('WtTypeA')
dat2['KOTypeA'].plot(ax=axes[0,1]); axes[0,1].set_title('KOTypeA')
dat2['WtTypeB'].plot(ax=axes[1,0]); axes[1,0].set_title('WtTypeB')
dat2['KOTypeB'].plot(ax=axes[1,1]); axes[1,1].set_title('KOTypeB')
plt.subplots_adjust(left=0.1,
bottom=0.1,
right=0.9,
top=0.9,
wspace=0.4,
hspace=0.4)

Advanced Bar Plots
Looking at one replicate a time
plt.figure();
dat2.iloc[1].plot(kind='bar'); plt.axhline(0, color='k')

Looking at all replicates at the same time
plt.figure();
dat2.plot(kind='bar'); plt.axhline(0, color='k')

plt.figure();
dat2.plot(kind='bar', colormap='Greens')

stacked boxes
dat3.plot(kind='bar', stacked=True);

Advanced Histogram
plt.figure()
dat.hist(by="genotype", figsize=(6, 4),bins=20)

Scatter Plot
from pandas.plotting import scatter_matrix
rpkm=pd.read_csv("C:\\Users\duan\Desktop\IntroductionToMatplotlib\\fakeExpressionDat.csv")
rpkm

scatter_matrix(rpkm, alpha=0.9, figsize=(6, 6), diagonal='kde')

Parallel Coordinates
Parallel coordinates is a plotting technique for plotting multivariate data. It allows one to see clusters in data and to estimate other statistics visually. Using parallel coordinates points are represented as connected line segments. Each vertical line represents one attribute. One set of connected line segments represents one data point. Points that tend to cluster will appear closer together
from pandas.plotting import parallel_coordinates
plt.figure()
parallel_coordinates(rpkm, 'pathway')

from pandas.plotting import parallel_coordinates
plt.figure()
parallel_coordinates(rpkm, 'pathway',colormap='gist_rainbow')

from pandas.plotting import parallel_coordinates
plt.figure()
parallel_coordinates(rpkm, 'pathway',colormap='spring')

from pandas.plotting import parallel_coordinates
plt.figure()
parallel_coordinates(rpkm, 'pathway',colormap='autumn')

Andrews Curves
Andrews Curves are smoothed versions of Parallel Coordinates
from pandas.plotting import andrews_curves
plt.figure()
andrews_curves(rpkm, 'pathway')
plt.show()

A potential issue when plotting a large number of columns is that it can be difficult to distinguish some series due to repetition in the default colors. To remedy this, we can either loop through different colors using rainbow() function. Or DataFrame plotting supports the use of the colormap= argument, which accepts either a Matplotlib colormap or a string that is a name of a colormap registered with Matplotlib
plt.figure()
andrews_curves(rpkm, 'pathway',color = [cm.rainbow(i) for i in np.linspace(0, 1, 3)])
plt.show()

plt.figure()
andrews_curves(rpkm, 'pathway',colormap='jet')
plt.show()

plt.figure()
andrews_curves(rpkm, 'pathway',colormap="winter")
plt.show()

RadViz
from pandas.plotting import radviz
plt.figure()
radviz(rpkm, 'pathway')
plt.show()

from pandas.plotting import radviz
plt.figure()
radviz(rpkm, 'pathway',colormap="Set1")
plt.show()

Last updated
Was this helpful?