본문 바로가기
ETC

Power BI 파이썬을 이용한 Radar Chart

by Wilkyway 2020. 9. 5.
반응형

 

Power BI의 파이썬 스크립트를 이용하여 Power BI에 없는 형태의 차트를 그려보았습니다.

 

1. 데이터를 마련해서 가져옵니다.

2. Py 버튼을 눌러서 Python 객체를 삽입합니다.

3. "필드" 탭에서 데이터를 "값"으로 불러오면, 스크립팅을 시작할 수 있습니다.

 

아래에 원본 데이터 및 예제 소스코드를 올려놓았으니, 참고하시기 바랍니다.

 

원본 데이터

cat value
Speed 90
Reliability 60
Comfort 65
Safety 70
Effieciency 40

 

Python Script 소스

# 데이터 프레임을 만들고 중복된 행을 제거하기 위한 다음 코드는 항상 실행되며 스크립트에 대한 프리앰블 역할을 합니다. 

# dataset = pandas.DataFrame(cat, value)
# dataset = dataset.drop_duplicates()

# 여기에 스크립트 코드를 붙여 넣거나 입력:
# -*- coding: utf-8 -*-

# Plots a radar chart.


from math import pi
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

#category=dataset.index
category=dataset.iloc[:,0].values

#칼럼의 값(value)
value=dataset.iloc[:,1].values

N = len(dataset.index)

x_as = [n / float(N) * 2 * pi for n in range(N)]


# Because our chart will be circular we need to append a copy of the first 
# value of each list at the end of each list with data

value = np.append(value, value[:1])
x_as += x_as[:1]

# Set color of axes
plt.rc('axes', linewidth=0.5, edgecolor="#888888")


# Create polar plot
ax = plt.subplot(111, polar=True)


# Set clockwise rotation. That is:
ax.set_theta_offset(pi / 2)
ax.set_theta_direction(-1)


# Set position of y-labels(degrees)
ax.set_rlabel_position(90)


# Set color and linestyle of grid
ax.xaxis.grid(True, color="#888888", linestyle='solid', linewidth=0.5)
ax.yaxis.grid(True, color="#333333", linestyle='dotted', linewidth=0.5)


# Set number of radial axes and remove labels
plt.xticks(x_as[:-1], [])

# Set yticks
plt.yticks([20, 40, 60, 80, 100], ["20", "40", "60", "80", "100"])


# Plot data
ax.plot(x_as, value, linewidth=0, linestyle='solid', zorder=3)

# Fill area
ax.fill(x_as, value, 'b', alpha=0.9)


# Set axes limits
plt.ylim(0, 100)


# Draw ytick labels to make sure they fit properly
for i in range(N):
       angle_rad = i / float(N) * 2 * pi

       if angle_rad == 0:
           ha, distance_ax = "center", 10
       elif 0 < angle_rad < pi:
           ha, distance_ax = "left", 1
       elif angle_rad == pi:
           ha, distance_ax = "center", 1
       else:
           ha, distance_ax = "right", 1

       ax.text(angle_rad, 100 + distance_ax, category[i], size=10, horizontalalignment=ha, verticalalignment="center")


# Show polar plot
plt.show()

pandas, 배열...이런거에 익숙치 않다보니 데이터 다루기가 너무 어려워서 한참 걸렸네요.

모두들 건승하시길~~

 

- 끝 - 

반응형

댓글