ヒストグラムしたい(hvplot.hist

1import pandas as pd
2import hvplot.pandas
3
4# data : pd.DataFrame
5data.hvplot.hist(
6    y = "energy_deposit",
7    xlabel="Energy Deposit [MeV]")

hvplot.histでヒストグラムを作成できます。 yに連続値をとるカラムを指定します。

ヒント

離散値(カテゴリカル)なカラムを指定したい場合は、 hvplot.barを使います。

Altairと比べて直感的に記述できるため、とても簡単です。

引数

公式のAPIドキュメントが見つからなかったため、ベタ書きしておきます。

1data.hvplot.hist(
2    y: str | list,    # カラム名 / 連続値
3    by: str | list,   # カラム名 / グループ化
4    bins: int,        # ビン数
5    bin_range: tuple = None,   # ビンの範囲
6    normed: bool = False,      # 規格化
7    cumulative: bool = False,  # 累積
8    alpha: float = 1.0         # 透過度
9)

タイトルしたい(title

1data.hvplot.hist(title="タイトル")
2data.hvplot.hist(fontsize={"title": "15pt"})

軸したい(xlabel / ylabel / clabel

 1
 2data.hvplot.hist(xlabel="x軸のタイトル")
 3data.hvplot.hist(xaxis="bottom")
 4data.hvplot.hist(xaxis="top")
 5data.hvplot.hist(ylabel="y軸のタイトル")
 6data.hvplot.hist(yaxis="left")
 7data.hvplot.hist(yaxis="right")
 8data.hvplot.hist(clabel="z軸のタイトル")
 9data.hvplot.hist(xticks=5, xformatter="%.3f")
10data.hvplot.hist(yticks=5, yformatter="%.3f")
11data.hvplot.hist(rot=45)
12data.hvplot.hist(shared_axis=True)

xlabelylabelclabelで軸のタイトルを変更できます。 xaxisyaxisで軸を表示する位置を変更できます。 xticksyticksで軸目盛りの間隔を変更できます。 また、xformatteryformatterで目盛り表示のフォーマットを変更できます。

凡例したい(legend

1data.hvplot.hist(legend=True)
2data.hvplot.hist(legend="top")
3data.hvplot.hist(legend="bottom")
4data.hvplot.hist(legend="left")
5data.hvplot.hist(legend="right")

凡例の表示/非表示を変更できます。 また、表示位置を変更できます。

対数したい

1data.hvplot.hist(logx=True)    # 片対数(x軸)
2data.hvplot.hist(logy=True)    # 片対数(y軸)
3data.hvplot.hist(logz=True)    # 片対数(z軸)
4data.hvplot.hist(loglog=True)  # 両対数(x軸とy軸)

logxlogylogzで片対数グラフに変更できます。 また、loglogで両対数グラフにできます。

キャンバスしたい

1data.hvplot.hist(width=700, height=300)
2data.hvplot.hist(max_width=700, max_height=700)
3data.hvplot.hist(min_width=300, min_height=300)
4data.hvplot.hist(responsive=True)
5data.hvplot.hist(frame_width=500, frame_height=500)
6data.hvplot.hist(padding=0.1)
7data.hvplot.hist(grid=True)
8data.hvplot.hist(bgcolor=None)

範囲したい

1data.hvplot.hist(xlim=(0, 500))
2data.hvplot.hist(ylim=(0, 500))
3data.hvplot.hist(clim=(0, 500))

x軸、y軸、z軸(=カラーバー)の範囲を変更できます。

カラーバーしたい

1data.hvplot.hist(colorbar=True)
2data.hvplot.hist(cnorm="linear")
3data.hvplot.hist(cnorm="log")
4data.hvplot.hist(cnorm="eq_hist")

フォントしたい(fontsize / fontscale

1data.hvplot.hist(fontsize=15)
2data.hvplot.hist(fontsize={"title": "15pt", "ylabel": "5pt", "ticks": 20})
3data.hvplot.hist(fontscale=1.5)

fontsizeでフォントサイズを変更できます。 辞書を使って、要素ごとにサイズを変更できます。 fontscaleですべてのフォントサイズをスケールできます。

リファレンス