Skip to content

파이썬 라이브러리 pygal #

Find similar titles

3회 업데이트 됨.

Edit
  • 최초 작성자
    Myunghee Jung
  • 최근 업데이트
    JEM

Structured data

Category
Programming

pygal #

Python을 이용해 차트를 그릴 수 있는 다양한 라이브러리 들이 있다. 그 중 pygal을 이용하면 SVG 차트를 그릴 수 있다.

pygal을 이용한 다양한 차트들 #

pygal에서는 14종류의 차트를 지원한다.

  • Line
  • Bar
  • Histogram
  • XY
  • Pie
  • Radar
  • Box
  • Dot
  • Funnel
  • SolidGauge
  • Gauge
  • Pyramid
  • Treemap
  • Maps

pygal을 이용해 차트 그리기 #

pygal 설치 #

pygal은 pip를 이용해 간단히 설치할 수 있다. (참고 : http://www.pygal.org/en/latest/installing.html)

pip install pygal

차트그리기 #

pygal을 설치하고 나서 본격적으로 간단한 bar chart를 그려보자

먼저 pygal를 import하고 Bar object를 생성한다.

#!python
import pygal    
bar_chart = pygal.Bar()

그리고, 생성된 Bar object에 chart로 나타내고자하는 value들을 넣는다.

#!python
bar_chart.add('number of car', [0, 10, 33, 56, 74, 99, 124, 643])

Bar object의 'render_to_file' 메소드를 이용하면 svg를 file형태로 생성할 수 있다.

#!python
bar_chart.render_to_file('barchart.svg')

'render_to_png' 메소드를 이용하면 png형태로 생성할 수 있다.

#!python
bar_chart.render_to_png('barchart.png')

다음과 같이 차트가 완성되었다.

https://googledrive.com/host/0B_8dj5V-KGlyQjRXTlo1SGxRVjg/barchart.svg

웹페이지에 임베딩하기 #

SVG파일로 만든 차트는 다음과 같은 방법으로 웹페이지에 임베딩 할 수 있다.

#!html
<html>
    <head>
        ...
    </head>
    <body>
        <figure>
            <embed type="image/svg+xml" src="barchart.svg" />
        </fugure>
    </body>
</html>

그 밖에, pygal - basic customization을 참고하면 취향에 맞는 다양한 customized chart를 생성할 수 있다.

References

http://pygal.org

Suggested Pages #

0.0.1_20230725_7_v68