빅데이터 분석가 양성과정/Python

iplot - 선 그래프 / 세부 요소 변경

분석가 황규진 2024. 7. 8. 13:09

선 그래프

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.rand(10,2), columns = ['A','B'])
df.head()

cf.help('scatter')
df.iplot(kind='scatter')

래프 모양 변경하기

df.iplot(kind = 'scatter', mode = 'lines+markers')

그래프 채우기

df.iplot(kind = 'scatter', fill = True)

 

세부요소

x축, y축, 그래프 타이틀 설정하기

df.iplot(kind = 'scatter',
         fill = True,
         xTitle = 'x title',
         yTitle = 'y title',
         title = 'title')

그래프 테마 변경

themes = cf.getThemes()
themes

['ggplot', 'pearl', 'solar', 'space', 'white', 'polar', 'henanigans']

for theme_item in themes:
    df.iplot(kind = 'scatter',
             theme = theme_item,
             fill = True,
             xTitle = 'x title',
             yTitle = 'y title',
             title = theme_item)

 

주요 요소

  • title, legend, xaxis, yaxis

변경방법

  • 타이틀 폰트 및 위치 변경
layout = {
    'title':
        {
            'text':'TTT',
            'font': {
                'family':'consolas',
                'size': 20,
                'color' : '#01579B' #google material palette 참조
            },
            'x' : 0.5,
            'y' : 0.85
        }
}
df.iplot(kind = 'scatter', layout = layout)

 

그래프 주요 세부 항목 변경

  • 그래프 타이틀 변경
  • 그래프 x축(xaxis), y축(yaxis) 타이틀 변경
  • x축(xaxis), y축(yaxis) tick 단위 변경 및 tick 노출 여부 설정
df.iplot(kind = 'scatter')

layout = {
    'title' :
        {
            'text' : '<b>Test Graph by Jae Yeong</b>',
            'font' : {
                'size' : 15,
                'color' : '#37474F'
            },
            'x' : 0.5,
            'y' : 0.88
        },
    'plot_bgcolor' : '#FAFAFA',
    'xaxis' : {
        'showticklabels' : True,
        'dtick' : 2,
        'title' : {
            'text' : 'X axis',
            'font' : {
                'size' : 10,
                'color' : '#37474F',
            }
        }
    },
    'yaxis': {
        'showticklabels' : True,
        'dtick' : 0.1,
        'title' : {
            'text' : 'Y axis',
            'font' : {
                'size' : 10,
                'color' : '#37474F',
            }
        }
    }
}
df.iplot(kind = 'scatter', mode = 'lines+markers', layout = layout)