728x90
plotly
plotly/datasets
In [1]:
# interactive graphs
import plotly.express as px
import pandas as pd
import numpy as np
In [2]:
df = px.data.gapminder();df
Out[2]:
country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
---|---|---|---|---|---|---|---|---|
0 | Afghanistan | Asia | 1952 | 28.801 | 8425333 | 779.445314 | AFG | 4 |
1 | Afghanistan | Asia | 1957 | 30.332 | 9240934 | 820.853030 | AFG | 4 |
2 | Afghanistan | Asia | 1962 | 31.997 | 10267083 | 853.100710 | AFG | 4 |
3 | Afghanistan | Asia | 1967 | 34.020 | 11537966 | 836.197138 | AFG | 4 |
4 | Afghanistan | Asia | 1972 | 36.088 | 13079460 | 739.981106 | AFG | 4 |
... | ... | ... | ... | ... | ... | ... | ... | ... |
1699 | Zimbabwe | Africa | 1987 | 62.351 | 9216418 | 706.157306 | ZWE | 716 |
1700 | Zimbabwe | Africa | 1992 | 60.377 | 10704340 | 693.420786 | ZWE | 716 |
1701 | Zimbabwe | Africa | 1997 | 46.809 | 11404948 | 792.449960 | ZWE | 716 |
1702 | Zimbabwe | Africa | 2002 | 39.989 | 11926563 | 672.038623 | ZWE | 716 |
1703 | Zimbabwe | Africa | 2007 | 43.487 | 12311143 | 469.709298 | ZWE | 716 |
1704 rows × 8 columns
In [3]:
df_italy = px.data.gapminder().query("country == 'Italy'")
g = px.bar(df_italy, x = 'year', y = 'pop')
g.show()
customizing
In [4]:
df.isnull().sum()
Out[4]:
country 0
continent 0
year 0
lifeExp 0
pop 0
gdpPercap 0
iso_alpha 0
iso_num 0
dtype: int64
In [5]:
g = px.bar(df_italy, x = 'year', y = 'pop', hover_data=['continent','gdpPercap'], color='lifeExp', labels={'Population/Life Exp of Italy'}, height = 600 )
g.show()
# hover_data : 사용하고자하는 값들의 목록
diabetes data
In [6]:
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/diabetes.csv")
df
Out[6]:
Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | Age | Outcome | |
---|---|---|---|---|---|---|---|---|---|
0 | 6 | 148 | 72 | 35 | 0 | 33.6 | 0.627 | 50 | 1 |
1 | 1 | 85 | 66 | 29 | 0 | 26.6 | 0.351 | 31 | 0 |
2 | 8 | 183 | 64 | 0 | 0 | 23.3 | 0.672 | 32 | 1 |
3 | 1 | 89 | 66 | 23 | 94 | 28.1 | 0.167 | 21 | 0 |
4 | 0 | 137 | 40 | 35 | 168 | 43.1 | 2.288 | 33 | 1 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
763 | 10 | 101 | 76 | 48 | 180 | 32.9 | 0.171 | 63 | 0 |
764 | 2 | 122 | 70 | 27 | 0 | 36.8 | 0.340 | 27 | 0 |
765 | 5 | 121 | 72 | 23 | 112 | 26.2 | 0.245 | 30 | 0 |
766 | 1 | 126 | 60 | 0 | 0 | 30.1 | 0.349 | 47 | 1 |
767 | 1 | 93 | 70 | 31 | 0 | 30.4 | 0.315 | 23 | 0 |
768 rows × 9 columns
In [7]:
g = px.scatter( data_frame= df, x = 'Age', y='BMI', color='Outcome')
g.show()
plotly graph objects
graph_object란
plotly express가 그래프 객체 위에 구하여 모두 그래프 객체의 그림을 출력으로 준다.
In [8]:
import plotly.graph_objects as go
Apple
In [9]:
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
df
Out[9]:
Date | AAPL.Open | AAPL.High | AAPL.Low | AAPL.Close | AAPL.Volume | AAPL.Adjusted | dn | mavg | up | direction | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2015-02-17 | 127.489998 | 128.880005 | 126.919998 | 127.830002 | 63152400 | 122.905254 | 106.741052 | 117.927667 | 129.114281 | Increasing |
1 | 2015-02-18 | 127.629997 | 128.779999 | 127.449997 | 128.720001 | 44891700 | 123.760965 | 107.842423 | 118.940333 | 130.038244 | Increasing |
2 | 2015-02-19 | 128.479996 | 129.029999 | 128.330002 | 128.449997 | 37362400 | 123.501363 | 108.894245 | 119.889167 | 130.884089 | Decreasing |
3 | 2015-02-20 | 128.619995 | 129.500000 | 128.050003 | 129.500000 | 48948400 | 124.510914 | 109.785449 | 120.763500 | 131.741551 | Increasing |
4 | 2015-02-23 | 130.020004 | 133.000000 | 129.660004 | 133.000000 | 70974100 | 127.876074 | 110.372516 | 121.720167 | 133.067817 | Increasing |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
501 | 2017-02-10 | 132.460007 | 132.940002 | 132.050003 | 132.119995 | 20065500 | 132.119995 | 114.494004 | 124.498666 | 134.503328 | Decreasing |
502 | 2017-02-13 | 133.080002 | 133.820007 | 132.750000 | 133.289993 | 23035400 | 133.289993 | 114.820798 | 125.205166 | 135.589534 | Increasing |
503 | 2017-02-14 | 133.470001 | 135.089996 | 133.250000 | 135.020004 | 32815500 | 135.020004 | 115.175718 | 125.953499 | 136.731280 | Increasing |
504 | 2017-02-15 | 135.520004 | 136.270004 | 134.619995 | 135.509995 | 35501600 | 135.509995 | 115.545035 | 126.723499 | 137.901963 | Decreasing |
505 | 2017-02-16 | 135.669998 | 135.899994 | 134.839996 | 135.350006 | 22118000 | 135.350006 | 116.203299 | 127.504333 | 138.805366 | Decreasing |
506 rows × 11 columns
Tesla
In [10]:
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/tesla-stock-price.csv")
In [11]:
df.sort_values('date',inplace=True)
In [12]:
fig = go.Figure([go.Scatter(name = 'Open',x = df['date'], y= df['open'], mode = 'markers', text = df['close'])])
fig.add_scatter(name = 'High',x = df['date'], y= df['high'], mode = 'lines')
fig.add_scatter(name = 'Low',x = df['date'], y= df['low'], mode = 'lines')
fig.update_layout(title='Tesla Stock Perfomance 2 year outdated')
fig.show()
plotly 3D
In [13]:
np.random.seed(1)
N = 70
fig = go.Figure(data=[go.Mesh3d(x=(55*np.random.randn(N)),
y=(65*np.random.randn(N)),
z=(35*np.random.randn(N)),
opacity = 0.5,
color='rgba(80,22,90,0.8)' )])
fig.update_layout(
scene = dict(xaxis = dict(nticks=5, range=[-200,200],),
yaxis = dict(nticks=5, range=[-150,200],),
zaxis = dict(nticks=5, range=[-200,200],),),
width = 850,
margin=dict(r=25, l=15, b=15, t=15))
fig.show()
# opcity : 불투명
728x90
'Data Analytics with python > [Data Analysis]' 카테고리의 다른 글
outlier (0) | 2023.02.18 |
---|---|
[Visualization] Plotly_Part2 (0) | 2023.01.22 |
[Visualization] seaborn (0) | 2023.01.22 |
[Visualization] matplotlib (0) | 2023.01.22 |
[Visualization] Basic_for _visualization (0) | 2023.01.22 |
댓글