반응형

Choosing Colormaps in Matplotlib

https://matplotlib.org/stable/tutorials/colors/colormaps.html

 

Choosing Colormaps in Matplotlib — Matplotlib 3.5.1 documentation

Colormaps are often split into several categories based on their function (see, e.g., [Moreland]): First, we'll show the range of each colormap. Note that some seem to change more "quickly" than others. Sequential2 Many of the \(L^*\) values from the Seque

matplotlib.org

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import cm
from colorspacious import cspace_converter
cmaps = {}

gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))


def plot_color_gradients(category, cmap_list):
    # Create figure and adjust figure height to number of colormaps
    nrows = len(cmap_list)
    figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22
    fig, axs = plt.subplots(nrows=nrows + 1, figsize=(6.4, figh))
    fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
                        left=0.2, right=0.99)
    axs[0].set_title(f'{category} colormaps', fontsize=14)

    for ax, name in zip(axs, cmap_list):
        ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
        ax.text(-0.01, 0.5, name, va='center', ha='right', fontsize=10,
                transform=ax.transAxes)

    # Turn off *all* ticks & spines, not just the ones with colormaps.
    for ax in axs:
        ax.set_axis_off()

    # Save colormap list for later.
    cmaps[category] = cmap_list

 

plot_color_gradients('Perceptually Uniform Sequential',
                     ['viridis', 'plasma', 'inferno', 'magma', 'cividis'])

plot_color_gradients('Sequential',
                     ['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
                      'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
                      'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn'])

 

 

 

 

 

반응형
반응형

[Python] 그래프에서 한글 깨질때


bar 차트를 그리기 위해서 먼저 필요한 모듈을 import합니다. matplotlib.pyplot과 numpy 모듈은 기존에도 자주 사용했지만 font_manager와 rc 모듈은 처음으로 import하는 모듈입니다. 해당 모듈은 그래프를 그릴 때 한글 폰트를 설정하기 위해 사용합니다.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import font_manager, rc

그림 15.23을 참조하면 업종명이 한글로 출력됨을 확인할 수 있습니다. matplotlib는 한글 폰트를 설정해주지 않으면 기본적으로 한글이 제대로 출력되지 않습니다. 따라서 데이터에 한글이 포함되어 있다면 다음과 같이 사용할 한글 폰트를 설정해줘야 한글이 정상적으로 출력됩니다.

font_name = font_manager.FontProperties(fname="c:/Windows/Fonts/malgun.ttf").get_name()
rc('font', family=font_name)

폰트를 설정하기 위해서는 폰트 이름을 알아야 합니다. 윈도우 7부터는 '맑은 고딕'이라는 폰트가 기본 폰트로 사용되고 있기 때문에, 본 예제에서는 'c:/Windows/Fonts/malgun.ttf'라는 경로를 사용했습니다. 폰트의 설치 경로로부터 폰트 이름을 먼저 알아낸 후 rc 함수를 통해서 폰트를 설정했습니다.






.

반응형

+ Recent posts