programing

x축 점에 대한 사용자 정의 텍스트로 그림 표시

bestprogram 2023. 8. 15. 11:17

x축 점에 대한 사용자 정의 텍스트로 그림 표시

아래 샘플 코드와 같이 matplotlib과 python을 사용하여 플롯을 그리고 있습니다.

x = array([0,1,2,3])
y = array([20,21,22,23])
plot(x,y)
show()

X축 위의 코드이므로 그려진 값이 표시됩니다.0.0, 0.5, 1.0, 1.5즉, 내 기준 x 값과 동일한 값입니다.

x의 각 점을 다른 문자열에 매핑할 방법이 있습니까?예를 들어 x축에 월 이름(문자열)을 표시합니다.Jun, July,...) 또는 사람 이름과 같은 다른 문자열("John", "Arnold", ...) 또는 시계 시간("12:20", "12:21", "12:22", ..).

제가 무엇을 할 수 있는지, 어떤 기능을 볼 수 있는지 아십니까?
내 목적을 위해서라면matplotlib.ticker도움이 필요합니까?

파이플롯을 사용하여 수동으로 xticks(및 yticks)를 설정할 수 있습니다.xxicks:

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0,1,2,3])
y = np.array([20,21,22,23])
my_xticks = ['John','Arnold','Mavis','Matt']
plt.xticks(x, my_xticks)
plt.plot(x, y)
plt.show()

이것은 저에게 효과가 있었습니다.X축의 매월

str_month_list = ['January','February','March','April','May','June','July','August','September','October','November','December']
ax.set_xticks(range(0,12))
ax.set_xticklabels(str_month_list)

더 자세한 예는 다음과 같습니다.


def plot_with_error_bands(x: np.ndarray, y: np.ndarray, yerr: np.ndarray,
                          xlabel: str, ylabel: str,
                          title: str,
                          curve_label: Optional[str] = None,
                          error_band_label: Optional[str] = None,
                          x_vals_as_symbols: Optional[list[str]] = None,
                          color: Optional[str] = None, ecolor: Optional[str] = None,
                          linewidth: float = 1.0,
                          style: Optional[str] = 'default',
                          capsize: float = 3.0,
                          alpha: float = 0.2,
                          show: bool = False
                          ):
    """
    note:
        - example values for color and ecolor:
            color='tab:blue', ecolor='tab:blue'
        - capsize is the length of the horizontal line for the error bar. Larger number makes it longer horizontally.
        - alpha value create than 0.2 make the error bands color for filling it too dark. Really consider not changing.
        - sample values for curves and error_band labels:
            curve_label: str = 'mean with error bars',
            error_band_label: str = 'error band',
    refs:
        - for making the seaborn and matplot lib look the same see: https://stackoverflow.com/questions/54522709/my-seaborn-and-matplotlib-plots-look-the-same
    """
    if style == 'default':
        # use the standard matplotlib
        plt.style.use("default")
    elif style == 'seaborn' or style == 'sns':
        # looks idential to seaborn
        import seaborn as sns
        sns.set()
    elif style == 'seaborn-darkgrid':
        # uses the default colours of matplot but with blue background of seaborn
        plt.style.use("seaborn-darkgrid")
    elif style == 'ggplot':
        # other alternative to something that looks like seaborn
        plt.style.use('ggplot')

    # ax = plt.gca()
    # fig = plt.gcf(
    # fig, axs = plt.subplots(nrows=1, ncols=1, sharex=True, tight_layout=True)
    # - if symbols in x axis instead of raw x value
    if x_vals_as_symbols is not None:
        # plt.xticks(x, [f'val{v}' for v in x]) to test
        plt.xticks(x, x_vals_as_symbols)
    # - plot bands
    plt.errorbar(x=x, y=y, yerr=yerr, color=color, ecolor=ecolor,
                 capsize=capsize, linewidth=linewidth, label=curve_label)
    plt.fill_between(x=x, y1=y - yerr, y2=y + yerr, alpha=alpha, label=error_band_label)
    plt.grid(True)
    if curve_label or error_band_label:
        plt.legend()
    plt.title(title)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)

    if show:
        plt.show()

예.

def plot_with_error_bands_xticks_test():
    import numpy as np  # v 1.19.2
    import matplotlib.pyplot as plt  # v 3.3.2

    # the number of x values to consider in a given range e.g. [0,1] will sample 10 raw features x sampled at in [0,1] interval
    num_x: int = 5
    # the repetitions for each x feature value e.g. multiple measurements for sample x=0.0 up to x=1.0 at the end
    rep_per_x: int = 5
    total_size_data_set: int = num_x * rep_per_x
    print(f'{total_size_data_set=}')
    # - create fake data set
    # only consider 10 features from 0 to 1
    x = np.linspace(start=0.0, stop=2*np.pi, num=num_x)

    # to introduce fake variation add uniform noise to each feature and pretend each one is a new observation for that feature
    noise_uniform: np.ndarray = np.random.rand(rep_per_x, num_x)
    # same as above but have the noise be the same for each x (thats what the 1 means)
    noise_normal: np.ndarray = np.random.randn(rep_per_x, 1)
    # signal function
    sin_signal: np.ndarray = np.sin(x)
    cos_signal: np.ndarray = np.cos(x)
    # [rep_per_x, num_x]
    y1: np.ndarray = sin_signal + noise_uniform + noise_normal
    y2: np.ndarray = cos_signal + noise_uniform + noise_normal

    y1mean = y1.mean(axis=0)
    y1err = y1.std(axis=0)
    y2mean = y2.mean(axis=0)
    y2err = y2.std(axis=0)

    x_vals_as_symbols: list[str] = [f'Val{v:0.2f}' for v in x]
    plot_with_error_bands(x=x, y=y1mean, yerr=y1err, xlabel='x', ylabel='y', title='Custom Seaborn', x_vals_as_symbols=x_vals_as_symbols)
    plot_with_error_bands(x=x, y=y2mean, yerr=y2err, xlabel='x', ylabel='y', title='Custom Seaborn', x_vals_as_symbols=x_vals_as_symbols)
    plt.show()

출력:

언급URL : https://stackoverflow.com/questions/3100985/plot-with-custom-text-for-x-axis-points