파이썬 시각화

2023. 11. 8. 17:34개발

    # create a function to plot bar chart

    def plot_bar_chart(column_name: str, hue: str=None, top_values: int = None, palette: str = "flare", show_grid : bool = True, 
                   figsize: tuple = (16, 9), bar_font_size: int=14, bar_angle: int=45, angle: int=45):
    fig = plt.figure(facecolor = (247/255, 247/255, 247/255), figsize = figsize)
    ax = plt.subplot(1, 1, 1)

    ax.set_facecolor((247/255, 247/255, 247/255))
    counts = None

    if top_values is not None:
        counts = df[column_name].value_counts().iloc[:top_values]
    else:
        counts = df[column_name].value_counts()

    bar_ax = sns.barplot(x = counts.keys(), y = counts, palette = palette, ax = ax, hue = df[hue] if hue is not None else None) 

    for i in bar_ax.containers:
        bar_ax.bar_label(i, rotation=bar_angle, fontsize = bar_font_size)

    plt.xticks(rotation = angle, fontsize = 14)
    plt.xlabel(f"{column_name}", fontweight="bold", fontsize = 18)
    plt.ylabel("count", fontweight="bold", fontsize = 18)
    plt.title(f"Top {top_values} {column_name}" if top_values is not None else f"{column_name}", fontsize = 27)
    if show_grid: plt.grid()
    plt.show()

#check more information and descriptive statistics for Quantitative variables

df.describe().iloc[1:, :].style.background_gradient()

image

### hlines(): 지정한 점을 따라 수평선을 표시합니다.
### vlines(): 지정한 점을 따라 수직선을 표시합니다
### vlines(xvalue, ymin, ymax, color, linestyle)

'개발' 카테고리의 다른 글

소수, 약수 구하기_에라토스테네스의 체  (0) 2023.11.06
머신러닝_시각화  (0) 2023.10.31
plotly_visualization 예시 코드  (1) 2023.10.30