Data 공부

[데이터 시각화] Python - text를 포함한 Radial Guage Chart 차트 본문

Data 공부/데이터 시각화

[데이터 시각화] Python - text를 포함한 Radial Guage Chart 차트

Junseokk 2023. 6. 12. 13:04

matplotlib을 통해 text를 포함한 원형 percent 차트그리는 방법입니다.


Input score 값에 따라  Radial Guage Chart가 표시됩니다.

 

예제 코드)

from math import pi
import numpy as np
from matplotlib.patches import Patch
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.font_manager as fm
from matplotlib.patches import FancyBboxPatch

def plot_score(score:int):
    '''
    plot score plot
    Args:
        score (int) : Score
    '''
    fig, ax = plt.subplots(figsize=(6, 6))
    ax = plt.subplot(projection='polar')
    data = [100, score]
    startangle = 90
    colors = ['#121CA7', '#0053CC']
    xs = [(i * pi *2)/ 100 for i in data]
    ys = [1, 1]
    left = (startangle * pi *2)/ 360 #this is to control where the bar starts
    
    # plot bars and points at the end to make them round
    ax.bar(10 * 2 * np.pi/100, -4, width = 2 * np.pi, color='#AAD2E6', edgecolor = None, alpha=0.2)
    ax.bar(10 * 2 * np.pi/100, 1, width = 2 * np.pi, color='#AAD2E6', edgecolor = None, alpha=0.2)
    ax.barh(ys[1], xs[1], left=left, height=0.8, color=colors[1], alpha=1, zorder=2)
    ax.barh(ys[0], xs[0], left=left, height=0.2, color=colors[0], alpha=0.2)

    ax.scatter(xs[1]+left, ys[1]-0.005, s=250, color=colors[1], zorder=2)
    ax.scatter(left+0.003, ys[1]+0.005, s=250, color=colors[1], zorder=2)
    

    plt.ylim(-4, 4)
    plt.text(-1.5, -3.5, score, ha='center', va='center', fontsize=90)

    plt.xticks([])
    plt.yticks([])
    
    ax.spines.clear()
    plt.show()
plot_score(90)

 

예제 PLOT)

<예제 Plot - Radial 차트>

반응형
Comments