Skip to content

Commit

Permalink
add(python-plot): basic usage of matplotlib
Browse files Browse the repository at this point in the history
issue #104
  • Loading branch information
sabertazimi committed Dec 15, 2018
1 parent 2baf34c commit 724d38f
Showing 1 changed file with 311 additions and 0 deletions.
311 changes: 311 additions & 0 deletions language/python/pythonBasicNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

- [Python Basic Notes](#python-basic-notes)
- [Matplotlib Usage](#matplotlib-usage)
- [Plot Type](#plot-type)
- [Basic Usage](#basic-usage)
- [Process Data File](#process-data-file)
- [Plot Style](#plot-style)
- [Plot Axis Tick](#plot-axis-tick)
- [Plot Legend](#plot-legend)
- [Subplot](#subplot)
- [Double y-axis](#double-y-axis)
- [3D Plot](#3d-plot)
- [Paper Figures Config](#paper-figures-config)
- [Paper Standard](#paper-standard)

<!-- /TOC -->

Expand All @@ -13,3 +24,303 @@
pip3 install matplotlib
pip3 install ggplot
```

### Plot Type

- bar plot
- line plot
- scatter plot
- pie plot
- stack plot
- histogram plot
- interval plot
- box plot
- KDE plot
- candlestick_ohlc plot

### Basic Usage

```python
import matplotlib.pyplot as plt

fig = plt.figure()
ax = plt.subplot2grid((1, 1), (0, 0))

ax.fill_between(x, y, y[0],where=(y > y[0]), facecolor='g', alpha=0.5)

ax.grid(True)
ax.xaxis.label.set_color('c') ax.yaxis.label.set_color('r') ax.set_yticks([0,25,50,75])
ax.xaxis.get_ticklabels()

ax.spines['left'].set_color('c') ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False) ax.spines['left'].set_linewidth(5)
ax.tick_params(axis='x', colors='#f06215')
ax.axhline(20, color='k', linewidth=5)

ax.text(x, y, 'text')
ax.annotate(
'Bad News!',
xytext=(0.8, 0.9),
textcoords='axes fraction',
arrowprops = dict(facecolor='grey', color='grey')
)

plt.plot(x, y, label='label', color='red', linewidth=5)
plt.bar(x, y, label='label', color='#111')
plt.scatter(x, y, label='label', marker='*', size=100)
plt.stackplot(x, y1, y2, ..., yn, colors=[])
plt.pie(slices, labels=[], colors=[], startangle=90, explode=(), autopct='%1.1f%%')
plt.hist(data, bins=10, histtype='bar', rwidth=0.8)

plt.xlabel('xlabel')
plt.ylabel('ylabel')
plt.title('title')
plt.legend()
plt.subplots_adjust()
plt.show()
```

## Process Data File

```python
import csv
x = []
y = []
with open('input.dat','r') as csvfile:
plots = csv.reader(csvfile, delimiter=',')
for row in plots:
x.append(int(row[0]))
y.append(int(row[1]))
```

```python
import numpy as np
x, y = np.loadtxt('input.dat', delimiter=',', unpack=True)
```

## Plot Style

```python
from matplotlib import style
print(plt.style.available)
print(plt.__file__)
style.use('ggplot')
style.use('fivethirtyeight')
plt.style.use('mystylesheet.mplrc')
```

## Plot Axis Tick

```python
ax3.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax3.xaxis.set_major_locator(mticker.MaxNLocator(10))
ax1.yaxis.set_major_locator(mticker.MaxNLocator(nbins=5, prune='lower'))

for label in ax3.xaxis.get_ticklabels():
label.set_rotation(45)

plt.setp(ax1.get_xticklabels(), visible=False)
plt.setp(ax2.get_xticklabels(), visible=False)
```

## Plot Legend

up middle legend

```python
ax.legend()
leg = ax.legend(loc=9, ncol=2,prop={'size':11})
leg.get_frame().set_alpha(0.4)
```

## Subplot

tall and witdh for grid template

```python
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(212)

ax1 = plt.subplot2grid((6,1), (0,0), rowspan=1, colspan=1)
ax2 = plt.subplot2grid((6,1), (1,0), rowspan=4, colspan=1)
ax3 = plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1)
```

### Double y-axis

```python
ax2v = ax2.twinx()
```

## 3D Plot

```python
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import style

style.use('ggplot')

fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')

x = [1,2,3,4,5,6,7,8,9,10]
y = [5,6,7,8,2,5,6,3,7,2]
z = [1,2,6,3,2,7,3,3,7,2]

x2 = [-1,-2,-3,-4,-5,-6,-7,-8,-9,-10]
y2 = [-5,-6,-7,-8,-2,-5,-6,-3,-7,-2]
z2 = [1,2,6,3,2,7,3,3,7,2]

ax1.scatter(x, y, z, c='g', marker='o')
ax1.scatter(x2, y2, z2, c ='r', marker='o')

ax1.set_xlabel('x axis')
ax1.set_ylabel('y axis')
ax1.set_zlabel('z axis')

plt.show()
```

```python
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import style
style.use('ggplot')

fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')

x3 = [1,2,3,4,5,6,7,8,9,10]
y3 = [5,6,7,8,2,5,6,3,7,2]
z3 = np.zeros(10)

dx = np.ones(10)
dy = np.ones(10)
dz = [1,2,3,4,5,6,7,8,9,10]

ax1.bar3d(x3, y3, z3, dx, dy, dz)


ax1.set_xlabel('x axis')
ax1.set_ylabel('y axis')
ax1.set_zlabel('z axis')

plt.show()
```

## Paper Figures Config

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

def latexify(fig_width=None, fig_height=None, columns=1):
'''Set up matplotlib's RC params for LaTeX plotting.
Call this before plotting a figure.
Parameters
----------
fig_width : float, optional, inches
fig_height : float, optional, inches
columns : {1, 2}
'''

# code adapted from http://www.scipy.org/Cookbook/Matplotlib/LaTeX_Examples

# Width and max height in inches for IEEE journals taken from
# computer.org/cms/Computer.org/Journal%20templates/transactions_art_guide.pdf

assert(columns in [1,2])

if fig_width is None:
fig_width = 3.39 if columns==1 else 6.9 # width in inches

if fig_height is None:
golden_mean = (sqrt(5)-1.0)/2.0 # Aesthetic ratio
fig_height = fig_width*golden_mean # height in inches

MAX_HEIGHT_INCHES = 8.0
if fig_height > MAX_HEIGHT_INCHES:
print("WARNING: fig_height too large:" + fig_height +
"so will reduce to" + MAX_HEIGHT_INCHES + "inches.")
fig_height = MAX_HEIGHT_INCHES

params = {'backend': 'ps',
'text.latex.preamble': ['\usepackage{gensymb}'],
'axes.labelsize': 8, # fontsize for x and y labels (was 10)
'axes.titlesize': 8,
'text.fontsize': 8, # was 10
'legend.fontsize': 8, # was 10
'xtick.labelsize': 8,
'ytick.labelsize': 8,
'text.usetex': True,
'figure.figsize': [fig_width,fig_height],
'font.family': 'serif'
}

matplotlib.rcParams.update(params)


def format_axes(ax):

for spine in ['top', 'right']:
ax.spines[spine].set_visible(False)

for spine in ['left', 'bottom']:
ax.spines[spine].set_color(SPINE_COLOR)
ax.spines[spine].set_linewidth(0.5)

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

for axis in [ax.xaxis, ax.yaxis]:
axis.set_tick_params(direction='out', color=SPINE_COLOR)

return ax

latexify()
ax.set_xlabel("X label")
ax.set_ylabel("Y label")
ax.set_title("Title")
plt.tight_layout()
format_axes(ax)
plt.savefig("figure.pdf")
```

```python
import numpy as np
import matplotlib as mpl
mpl.use('pdf')
import matplotlib.pyplot as plt

plt.rc('font', family='serif', serif='Times')
plt.rc('text', usetex=True)
plt.rc('xtick', labelsize=8)
plt.rc('ytick', labelsize=8)
plt.rc('axes', labelsize=8)

# width as measured in inkscape
width = 3.487
height = width / 1.618

fig, ax = plt.subplots()
fig.subplots_adjust(left=.15, bottom=.16, right=.99, top=.97)

x = np.arange(0.0, 3*np.pi , 0.1)
plt.plot(x, np.sin(x))

ax.set_ylabel('Some Metric (in unit)')
ax.set_xlabel('Something (in unit)')
ax.set_xlim(0, 3*np.pi)

fig.set_size_inches(width, height)
fig.savefig('plot.pdf')
```

## Paper Standard

- [Matplotlib for Papers](https://github.com/jbmouret/matplotlib_for_papers)

0 comments on commit 724d38f

Please sign in to comment.