4 분 소요

[Notice] [visualization_practice_3]

Matplotlib (save graph and use it)

plt.savefig(‘my_graph.png’, dpi = 300)

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.plot(np.arange(10), np.arange(10)*2, marker = 'o', linestyle = '', color = 'b', alpha = 0.1)
plt.plot(np.arange(10), np.arange(10)*2 - 10, marker = 'o', linestyle = '-', color = 'c', alpha = 0.3)
plt.plot(np.arange(10), np.arange(10)*2 - 20, marker = 'v', linestyle = '--', color = 'y', alpha = 0.6)
plt.plot(np.arange(10), np.arange(10)*2 - 30, marker = '+', linestyle = '-.', color = 'r', alpha = 1.0)
#title and font size
plt.title('my_graph', fontsize = 20)

#X,Y axis Label
plt.xlabel('X axis', fontsize = 20)
plt.ylabel('Y axis', fontsize = 20)

#x tick, y tick setting
plt.xticks(rotation = 90)
plt.yticks(rotation = 30)

#grid setting
plt.grid()

#save image - savefig
plt.savefig("my_graph.png", dpi = 300)


plt.show()

Use matplotlib

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rc('font', family = 'NanumBarunGothic')
plt.rcParams["figure.figsize"] = (12, 9)

Scatterplot

np.random.rand(50)
array([0.48298281, 0.4179641 , 0.16334523, 0.3992768 , 0.70315133,
       0.57085869, 0.77896308, 0.7054962 , 0.28213664, 0.28017167,
       0.57054743, 0.67824642, 0.97116605, 0.20092206, 0.77682691,
       0.59216747, 0.57699709, 0.2706546 , 0.82324854, 0.97268348,
       0.93242201, 0.18295371, 0.56316071, 0.92372413, 0.29448649,
       0.47777773, 0.55394811, 0.38182811, 0.71925979, 0.95483394,
       0.03964943, 0.85657902, 0.48496161, 0.88190472, 0.94278865,
       0.50259586, 0.77664273, 0.72085393, 0.11544711, 0.96985862,
       0.52707684, 0.31688735, 0.78463002, 0.79158512, 0.93694118,
       0.1980533 , 0.96344072, 0.98625565, 0.59471482, 0.09360594])
np.arange(50)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49])

1-1 set x,y,colors, area

  • color converts an arbitrary value to a color value.

  • area represents the area of a point, and if the value increases, the area also increases.

x = np.random.rand(50)
y = np.random.rand(50)
colors = np.arange(50)
area = x * y * 1000
plt.scatter(x,y, s = area, c = colors)
plt.show()
findfont: Font family ['NanumBarunGothic'] not found. Falling back to DejaVu Sans.

1-2 cmap and alpha

  • If you specify a color in cmap, you can also take all the color values the same.

  • The alpha value indicates transparency, and a value between 0 and 1 can be specified, and the closer to 0 that means more transparent it is.

np.random.rand(50)
array([0.28295099, 0.45602094, 0.32557071, 0.61196114, 0.46427488,
       0.01706604, 0.36521701, 0.68947206, 0.62023533, 0.00246025,
       0.63281304, 0.78165904, 0.45255827, 0.69682756, 0.21008187,
       0.38032342, 0.77418079, 0.03162567, 0.47971165, 0.01652912,
       0.10476231, 0.49657065, 0.45311136, 0.5419424 , 0.9454405 ,
       0.90920679, 0.05632493, 0.46871966, 0.59234766, 0.72183894,
       0.50649515, 0.43014363, 0.0128534 , 0.18483497, 0.93381698,
       0.44320663, 0.88620452, 0.17384716, 0.0389346 , 0.37549767,
       0.87217889, 0.98671104, 0.46099159, 0.12615308, 0.12907595,
       0.06907589, 0.30473651, 0.72993749, 0.42512292, 0.56846879])
plt.figure(figsize=(12, 6))

plt.subplot(131)
plt.scatter(x, y, s=area, cmap='blue', alpha=0.1)
plt.title('alpha=0.1')

plt.subplot(132)
plt.title('alpha=0.5')
plt.scatter(x, y, s=area, cmap='blue', alpha=0.5)

plt.subplot(133)
plt.title('alpha=1.0')
plt.scatter(x, y, s=area, cmap='blue', alpha=1.0)

plt.show()
findfont: Font family ['NanumBarunGothic'] not found. Falling back to DejaVu Sans.

Barplot, Barhplot

Drawing Basic Barplot

x = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Physics']
y = [66, 80, 60, 50, 80, 10]

plt.figure(figsize=(6, 3))

# plt.bar(x, y)
plt.bar(x, y, align='center', alpha=0.7, color='k')
plt.xticks(x, rotation = 30)
plt.ylabel('Number of Students')
plt.title('Subjects')

plt.show()

Drawing Basic Barhplot

x = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Physics']
y = [66, 80, 60, 50, 80, 10]

plt.barh(x, y, align='center', alpha=0.7, color='b')
plt.yticks(x)
plt.xlabel('Number of Students')
plt.title('Subjects')

plt.show()

Drawing comparison graphs in Batplot

x_label = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Physics']
x = np.arange(len(x_label))
y_1 = [66, 80, 60, 50, 80, 10]
y_2 = [55, 90, 40, 60, 70, 20]

# set up width
width = 0.35

# maek subplots
fig, axes = plt.subplots()

# set up width
axes.bar(x - width/2, y_1, width, align='center', alpha=0.5)
axes.bar(x + width/2, y_2, width, align='center', alpha=0.8)

# set up xticks
plt.xticks(x)
axes.set_xticklabels(x_label)
plt.ylabel('Number of Students')
plt.title('Subjects')

plt.legend(['Dylan', 'Alice'])

plt.show()

x_label = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Physics']
x = np.arange(len(x_label))
y_1 = [66, 80, 60, 50, 80, 10]
y_2 = [55, 90, 40, 60, 70, 20]

# set up width
width = 0.35

# make subplots
fig, axes = plt.subplots()

# set up width
axes.barh(x - width/2, y_1, width, align='center', alpha=0.5, color='k')
axes.barh(x + width/2, y_2, width, align='center', alpha=0.8, color='c')

# set up yricks
plt.yticks(x)
axes.set_yticklabels(x_label)
plt.xlabel('Number of Students')
plt.title('Subjects')

plt.legend(['Dylan', 'Alice'])

plt.show()

Lineplot

x = np.arange(0, 10, 0.1)
y = 1 + np.log(x)

plt.plot(x, y)

plt.xlabel('x value', fontsize=15)
plt.ylabel('y value', fontsize=15)
plt.title('log graph', fontsize=18)

plt.grid()

plt.show()
:2: RuntimeWarning: divide by zero encountered in log
  y = 1 + np.log(x)
findfont: Font family ['NanumBarunGothic'] not found. Falling back to DejaVu Sans.
findfont: Font family ['NanumBarunGothic'] not found. Falling back to DejaVu Sans.
</pre>



```python
x = np.arange(0, 10, 0.1)
y_1 = 1 + np.sin(x)
y_2 = 1 + np.cos(x)

plt.plot(x, y_1, label='1+sin', color='blue', alpha=0.3)
plt.plot(x, y_2, label='1+cos', color='red', alpha=0.7)

plt.xlabel('x value', fontsize=15)
plt.ylabel('y value', fontsize=15)
plt.title('sin and cos graph', fontsize=18)

plt.grid()
plt.legend()

plt.show()
```



### Marker



```python
x = np.arange(0, 10, 0.1)
y_1 = 1 + np.sin(x)
y_2 = 1 + np.cos(x)

plt.plot(x, y_1, label='1+sin', color='blue', alpha=0.3, marker='o')
plt.plot(x, y_2, label='1+cos', color='red', alpha=0.7, marker='+')

plt.xlabel('x value', fontsize=15)
plt.ylabel('y value', fontsize=15)
plt.title('sin and cos graph', fontsize=18)

plt.grid()
plt.legend()

plt.show()
```



### Line style



```python
x = np.arange(0, 10, 0.1)
y_1 = 1 + np.sin(x)
y_2 = 1 + np.cos(x)

plt.plot(x, y_1, label='1+sin', color='blue', linestyle=':')
plt.plot(x, y_2, label='1+cos', color='red', linestyle='-.')

plt.xlabel('x value', fontsize=15)
plt.ylabel('y value', fontsize=15)
plt.title('sin and cos graph', fontsize=18)

plt.grid()
plt.legend()

plt.show()
```


댓글남기기