> For the complete documentation index, see [llms.txt](https://igb.mit.edu/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://igb.mit.edu/mini-courses/python/data-processing-with-python/matplotlib/basic-plotting.md).

# Basic Plotting

### Getting Ready

#### Import packages

```
import numpy as np
```

```
from numpy.random import randn
```

```
import matplotlib.pyplot as plt
```

```
import pandas as pd
```

```
from pandas import Series, DataFrame,date_range
```

```
from matplotlib import cm
```

#### Read in data

```
dat=pd.read_csv("C:\\Users\duan\Desktop\PythonDataProcessingVisualization\metaDataMean.txt", sep='\s+')
```

```
dat
```

<figure><img src="https://498238201-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWuHhstIreJ3jFvE4gQ3y%2Fuploads%2FRiOqkPntPdvCCcmS6Eqd%2Fimage.png?alt=media&amp;token=b175d164-3b1b-4076-befb-5a4211f6a35e" alt=""><figcaption></figcaption></figure>

### Line Plot

```
%matplotlib inline
```

```
plt.figure()
dat.samplemeans.plot()
```

<figure><img src="https://498238201-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWuHhstIreJ3jFvE4gQ3y%2Fuploads%2Fx9gjfkhgeyEoFMhIWIeL%2Fimage.png?alt=media&amp;token=9f964174-6cc0-4c0e-a8e2-2b46b5997b98" alt=""><figcaption></figcaption></figure>

```
plt.figure()
dat.samplemeans.plot(secondary_y=True, style='g')
```

<figure><img src="https://498238201-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWuHhstIreJ3jFvE4gQ3y%2Fuploads%2FNEKjJvNamDsxweAlJhqR%2Fimage.png?alt=media&amp;token=14567b0d-0d99-4668-8590-b261eca55477" alt=""><figcaption></figcaption></figure>

For list of named colors see [here](https://matplotlib.org/stable/gallery/color/named_colors.html)

Change plotting style and add legend

```
plt.figure();dat.samplemeans.plot(style='k--', label='sample means'); plt.legend()
```

<figure><img src="https://498238201-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWuHhstIreJ3jFvE4gQ3y%2Fuploads%2Ft1vDIHUfeR9xdG69FVSm%2Fimage.png?alt=media&amp;token=0b2e1e12-a74c-4e41-be9b-841f1e522118" alt=""><figcaption></figcaption></figure>

### Bar Plot

```
plt.figure();
dat.samplemeans.plot(kind='bar')
```

<figure><img src="https://498238201-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWuHhstIreJ3jFvE4gQ3y%2Fuploads%2FmCVR2WPNnRBF0w8TjdFj%2Fimage.png?alt=media&amp;token=eef387a2-9d73-447a-827f-c66b1393986e" alt=""><figcaption></figcaption></figure>

\
add a horizontal line

```
plt.figure();
dat.samplemeans.plot(kind='bar'); plt.axhline(10,linewidth=4, color='r')
```

<figure><img src="https://498238201-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWuHhstIreJ3jFvE4gQ3y%2Fuploads%2FAy1w3ke1bnL7DgsTaYLQ%2Fimage.png?alt=media&amp;token=e005c315-96cb-4cd6-a4cc-74e0858d2fc0" alt=""><figcaption></figcaption></figure>

plot horizontal bars

```
plt.figure();
dat.samplemeans.plot(kind='barh',color="pink")
```

<figure><img src="https://498238201-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWuHhstIreJ3jFvE4gQ3y%2Fuploads%2F2PMOdqM3n5dHQRK7EaKD%2Fimage.png?alt=media&amp;token=b8e707be-7c1b-438e-86b3-44e9dad10bd9" alt=""><figcaption></figcaption></figure>

```
plt.figure();
dat.samplemeans.plot(kind='barh',color = [cm.rainbow(i) for i in np.linspace(0, 1, len(dat.samplemeans))])
```

<figure><img src="https://498238201-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWuHhstIreJ3jFvE4gQ3y%2Fuploads%2FUKmVjSZHrBxOOYSZNWjI%2Fimage.png?alt=media&amp;token=e951d91d-8966-4125-9d8d-834be16e7eb3" alt=""><figcaption></figcaption></figure>

### Histogram Plot

```
plt.figure();
dat.samplemeans.hist()
```

<figure><img src="https://498238201-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWuHhstIreJ3jFvE4gQ3y%2Fuploads%2Fx6x2QAVK8QOM5RHpJ6px%2Fimage.png?alt=media&amp;token=7cf25b08-6093-4533-a5af-8714e292319f" alt=""><figcaption></figcaption></figure>

### Box Plot

```
plt.figure();
bp = dat.boxplot()
```

<figure><img src="https://498238201-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWuHhstIreJ3jFvE4gQ3y%2Fuploads%2FGEmD7NrshdY8DpMO5kXh%2Fimage.png?alt=media&amp;token=b62eee91-7080-4260-8322-4c80a4089699" alt=""><figcaption></figcaption></figure>

Define a title

```
plt.figure();
bp=dat.boxplot()
bp.set_title('My box plot')
```

<figure><img src="https://498238201-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWuHhstIreJ3jFvE4gQ3y%2Fuploads%2F1Bl9ATGuDTJCj2aNzVuT%2Fimage.png?alt=media&amp;token=f67e34bd-6db8-42e2-9259-6c5747055c12" alt=""><figcaption></figcaption></figure>

Boxplot by celltype

```
plt.figure();
bp = dat.boxplot(by='genotype')
```

<figure><img src="https://498238201-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWuHhstIreJ3jFvE4gQ3y%2Fuploads%2F8GKWtXAnxooXVmBqFow1%2Fimage.png?alt=media&amp;token=f5ab9e3d-e201-4177-872d-bedefdbd8e5b" alt=""><figcaption></figcaption></figure>

Better layout by excluding the automatic title

```
plt.figure();
bp = dat.boxplot(by='genotype')
plt.suptitle('') # that's what you're after
plt.show()
```

<figure><img src="https://498238201-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWuHhstIreJ3jFvE4gQ3y%2Fuploads%2Fb9CY7L6CoHCDn4RGo0vJ%2Fimage.png?alt=media&amp;token=adc22c79-9007-4405-93fe-a02219b6e59a" alt=""><figcaption></figcaption></figure>

More sophisticated plotting can better reveal the trend

```
plt.figure();
bp = dat.boxplot(column=['samplemeans'], by=['celltype','genotype'])
title_boxplot = 'expression means'
plt.title( title_boxplot )
plt.suptitle('')
plt.show()
```

<figure><img src="https://498238201-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWuHhstIreJ3jFvE4gQ3y%2Fuploads%2FrLd8st8qLiM83TiCOxdS%2Fimage.png?alt=media&amp;token=3bd4eee8-1168-47f1-b188-2deaa50e8d46" alt=""><figcaption></figcaption></figure>
