728x90
matplotlib tutorial
https://matplotlib.org/stable/tutorials/introductory/usage.html
In [37]:
import matplotlib.pyplot as plt
import seaborn as sns # bulit on top of matplotlib
import pandas as pd
import numpy as np
%matplotlib inline
In [38]:
plt.plot([1,2,3,4,5,6], [9,7,8,2,4,6])
Out[38]:
[<matplotlib.lines.Line2D at 0x7f1c73ce51c0>]
In [39]:
fig, axs = plt.subplots(2, 2)
In [40]:
x = np.linspace(0, 1, 50) # sample
fig, axs = plt.subplots(1, 2, figsize=(10,5))
axs[0].plot(x, x, label = 'linear')
axs[1].plot(x, x**2, label = 'quadratic')
Out[40]:
[<matplotlib.lines.Line2D at 0x7f1c73b8b640>]
In [41]:
x = np.linspace(0, 1, 50) # sample
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10,5))
ax1.plot(x, x, label = 'linear')
ax2.plot(x, x**3, label = 'cubic')
Out[41]:
[<matplotlib.lines.Line2D at 0x7f1c73ac0fd0>]
barplot
In [42]:
data = [50, 100, 150, 200, 250 ]
plt.bar(range(len(data)), data, color = 'b')
Out[42]:
<BarContainer object of 5 artists>
stacked bar chart
In [43]:
plt.style.use('ggplot')
labels = ['Test 1', 'Test 2', 'Test 3', 'Test 4', 'Test 5', 'Test 6',]
price_one = [10, 20, 30, 40, 20, 10]
price_two = [10, 20, 50, 40, 30, 20]
tax_one = [1,2,1,2,1,1]
tax_two = [3,4,3,4,3,4]
width = 0.50
# stacked bar visualization
fig, ax = plt.subplots()
ax.bar(labels, price_one, width, align = 'edge', yerr=tax_one, label='First Label')
ax.bar(labels, price_two, width, align = 'edge', yerr=tax_two, bottom=price_one, label='Second Label')
# align : x축 좌표에 정렬( 'center', 'edge' )
# yerr : y축 error bar
ax.set_ylabel('Value')
ax.set_title('Scores by Group')
plt.xticks(rotation='45')
ax.legend()
plt.show()
In [44]:
plt.style.use('ggplot')
data = {'Snack':20, "beverages":10, 'meat': 25, 'vegetable': 15}
names = list(data.keys())
values = list(data.values())
fig, axs = plt.subplots(1,3, figsize=(8,8), sharey=True)
axs[0].bar(names, values)
axs[0].set_xticklabels(ax.get_xticks(), rotation = 50)
axs[1].scatter(names, values)
axs[1].set_xticklabels(ax.get_xticks(), rotation = 50)
axs[2].plot(names, values)
fig.suptitle('Plotting')
plt.xticks(rotation=45)
Out[44]:
([0, 1, 2, 3], <a list of 4 Text major ticklabel objects>)
In [45]:
plt.style.use('dark_background')
x = np.linspace(2, 10 * np.pi, 50)
y = np.sin(x ** 2)
fig, axs = plt.subplots(2)
axs[0].plot(x,y, color='white')
axs[1].plot(x,y**2)
Out[45]:
[<matplotlib.lines.Line2D at 0x7f1c73848430>]
Practical Example
In [46]:
import datetime
In [47]:
df = pd.read_csv('Bitcoin.csv')
df.head()
Out[47]:
Date | Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|---|
0 | 2014-09-17 | 465.864014 | 468.174011 | 452.421997 | 457.334015 | 457.334015 | 21056800 |
1 | 2014-09-18 | 456.859985 | 456.859985 | 413.104004 | 424.440002 | 424.440002 | 34483200 |
2 | 2014-09-19 | 424.102997 | 427.834991 | 384.532013 | 394.795990 | 394.795990 | 37919700 |
3 | 2014-09-20 | 394.673004 | 423.295990 | 389.882996 | 408.903992 | 408.903992 | 36863600 |
4 | 2014-09-21 | 408.084991 | 412.425995 | 393.181000 | 398.821014 | 398.821014 | 26580100 |
In [48]:
plt.style.use('ggplot')
ax = df['Volume'].plot.hist()
In [49]:
df['Date'] = pd.to_datetime(df['Date'], format='%Y/%m/%d')
In [51]:
df['year'] = df['Date'].dt.year
df['day'] = df["Date"].dt.day
In [53]:
df_solution = df[['year', 'day', 'Volume' ]]
In [54]:
fig, ax = plt.subplots()
bitcoin_plot = df_solution.plot.scatter(x='year', y='day', c='Volume', colormap='viridis', figsize=(20,10), ax=ax )
In [56]:
from google.colab import files
fig, ax = plt.subplots()
bitcoin_plot = df_solution.plot.scatter(x='year', y='day', c='Volume', colormap='viridis', figsize=(20,10), ax=ax )
plt.savefig('test_matplotlib.png', bbox_inches='tight')
files.download('test_matplotlib.png')
728x90
'Data Analytics with python > [Data Analysis]' 카테고리의 다른 글
[Visualization] Plotly_Part1 (0) | 2023.01.22 |
---|---|
[Visualization] seaborn (0) | 2023.01.22 |
[Visualization] Basic_for _visualization (0) | 2023.01.22 |
[Text]S8_08_Word_Cloud (0) | 2023.01.21 |
[Text]S8_07_Text_visualization (0) | 2023.01.21 |
댓글