# 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="/files/CXw8rx3E63dQzvtrPjFj" alt=""><figcaption></figcaption></figure>

### Line Plot

```
%matplotlib inline
```

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

<figure><img src="/files/a5XvtXDvGBNu6BqTdQi1" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/C62NVL45MWk0ooMb0rjf" 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="/files/zCXPtS3Itevb40qUGSKF" alt=""><figcaption></figcaption></figure>

### Bar Plot

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

<figure><img src="/files/N1OMsY9EGczgbF0ILOV1" 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="/files/LQg7VgMhHoD6tjfaS2CV" alt=""><figcaption></figcaption></figure>

plot horizontal bars

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

<figure><img src="/files/GzoohfL8GEZsxZHsO79W" 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="/files/kansS8VPALndpNv0fzX4" alt=""><figcaption></figcaption></figure>

### Histogram Plot

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

<figure><img src="/files/MUNNedTv6qIgx5sEhf7E" alt=""><figcaption></figcaption></figure>

### Box Plot

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

<figure><img src="/files/mKSN5OXku0JGjtLqNBU6" alt=""><figcaption></figcaption></figure>

Define a title

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

<figure><img src="/files/3wos6JMXL2STG4OJuCCS" alt=""><figcaption></figcaption></figure>

Boxplot by celltype

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

<figure><img src="/files/uaGTwAnidG7Ae8BXsMXi" 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="/files/9rAkbFPplW4tuZ9gH5Re" 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="/files/lCeamUw2ldukhIZKADaj" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://igb.mit.edu/mini-courses/python/data-processing-with-python/matplotlib/basic-plotting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
