---
# System prepended metadata

title: '13.Wednesday, 31-07-2019, Draw chart by Matplotlib'

---

# 13.Wednesday, 31-07-2019, Draw chart by Matplotlib
### Needed library
```python
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
%matplotlib inline
mpl.style.use('default')
```

- We need to convert Data Frame to list before drawing chart
```python
profitList = df ['total_profit'].tolist()
```
### Chart

*Figure*

|Operator|Description|
|--- |--- |
|`fig = plt.figures()`|a container that contains all plot elements|

*Axes*

|Operator|Description|
|--- |--- |
|`fig.add_axes()` |Initializes subplot |
|`a = fig.add_subplot(222)` | A subplot is an axes on a grid system row-col-num.|
|`fig, b = plt.subplots(nrows=3, nclos=2)`|Adds subplot|
|`ax = plt.subplots(2, 2)`|Creates subplot: 2 rows & 2 cols|

**Plotting**

|Operator|Description|
|--- |--- |
|`lines = plt.plot(x,y)`|Plot data connected by lines|
|`plt.scatter(x,y)`|Creates a scatterplot, unconnected data points|
|`plt.bar(xvalue, data , width, color...)`|simple vertical bar chart|
|`plt.barh(yvalue, data, width, color...)`|simple horizontal bar|
|`plt.hist(x, y)`|Plots a histogram|
|`plt.boxplot(x,y)`|Box and Whisker plot|
|`plt.violinplot(x, y)`|Creates violin plot|
|`ax.fill(x, y, color='lightblue')`;`ax.fill_between(x,y,color='yellow')`|Fill area under/between plots|
|`plt.stackplot(x, y1, y2,...,yn, colors=list_of_color, labels=list_of_lables)`|Creates Stack plot|
*Limits*

|Operators|Description|
|--- |--- |
|`plt.xlim(0, 7)`|Sets x-axis to display 0 - 7|
|`plt.ylim(-0.5, 9)`|Sets y-axis to display -0.5 - 9|
|`ax.set(xlim=[0, 7], ylim=[-0.5, 9])`;`ax.set_xlim(0, 7)`|Sets limits|
|`plt.margins(x=1.0, y=1.0)`|Set margins: add padding to a plot, values 0 - 1|
|`plt.axis('equal')`|Set the aspect ratio of the plot to 1|

*Legend/labels*

|Operator|Description|
|--- |--- |
|`plt.title('just a title')`|Sets title of plot|
|`plt.xlabel('x-axis')`|Sets label next to x-axis|
|`plt.ylabel('y-axis')`|Sets label next to y-axis|
|`ax.set(title='axis', ylabel='Y-Axis', xlabel='X-Axis')`|Set title and axis labels|
|`ax.legend(loc='best')`|Legend is used to show label of each plot. No overlapping plot elements|

*Tick*

|Operator|Description|
|--- |--- |
|`plt.xticks(x, labels, rotation='vertical')`|Set ticks|
|`ax.xaxis.set(ticks=range(1,5), ticklabels=[3,100,-12,"foo"])`|Set x-ticks - The information in X-Axis|
|`ax.tick_params(axis='y', direction='inout', length=10)`|Make y-ticks longer and go in and out|

Examples:
https://colab.research.google.com/drive/1S6Sfmo46f_pqc-HzelN30LiFlVdvkV_6


### Simple elements
- Write label
```python
plt.xlabel('x label')
plt.ylabel('y label')

- Other way
fig, ax = plt.subplots()
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
```
- Write Title of chart
```python
plt.title('Simple plot')
```

- Write a text
```python
ax.text(3, 8, 'boxed italics text in data coords', style='italic',
        bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})

* Detail:
- x
- y
- content
- style
- format of the frame
```
- Write a point
```python
ax.plot([2], [1], 'o')
```
- Write an arrow
```python
ax.annotate('annotate', xy=(2, 1), xytext=(2, 3),
            arrowprops=dict(facecolor='black', shrink=1))
```

