<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>「ヒストグラム」タグの記事一覧Python Tech</title>
	<atom:link href="https://tech.nkhn37.net/tag/%E3%83%92%E3%82%B9%E3%83%88%E3%82%B0%E3%83%A9%E3%83%A0/feed/" rel="self" type="application/rss+xml" />
	<link>https://tech.nkhn37.net</link>
	<description>Python学習サイト</description>
	<lastBuildDate>Sun, 11 Jan 2026 21:23:22 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://tech.nkhn37.net/wp-content/uploads/2021/01/cropped-lion-normal-clear-1-32x32.png</url>
	<title>「ヒストグラム」タグの記事一覧Python Tech</title>
	<link>https://tech.nkhn37.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【matplotlib】ヒストグラムの描画方法 ~ hist, hist2d, hexbin ~</title>
		<link>https://tech.nkhn37.net/matplotlib-hist-hist2d-hexbin/</link>
					<comments>https://tech.nkhn37.net/matplotlib-hist-hist2d-hexbin/#respond</comments>
		
		<dc:creator><![CDATA[naoki-hn]]></dc:creator>
		<pubDate>Wed, 27 Oct 2021 00:00:00 +0000</pubDate>
				<category><![CDATA[matplotlib]]></category>
		<category><![CDATA[hexbin]]></category>
		<category><![CDATA[hist]]></category>
		<category><![CDATA[hist2d]]></category>
		<category><![CDATA[ヒストグラム]]></category>
		<guid isPermaLink="false">https://tech.nkhn37.net/?p=1943</guid>

					<description><![CDATA[Pythonによるデータ分析をする際にグラフなどの可視化でよく利用されるmatplotlibでヒストグラムを描画する方法について解説します。 ヒストグラムの描画方法 matplotlibでヒストグラムを描画する際のメソッ [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Pythonによるデータ分析をする際にグラフなどの可視化でよく利用される<span class="marker"><strong>matplotlib</strong></span>で<span class="marker"><strong>ヒストグラムを描画する方法</strong></span>について解説します。</p>



<h2 class="wp-block-heading jinr-heading d--bold" id="ヒストグラムの描画方法">ヒストグラムの描画方法</h2>



<p>matplotlibでヒストグラムを描画する際のメソッドとしては、<span class="marker"><strong>hist</strong></span>, <span class="marker"><strong>hist2d</strong></span>, <span class="marker"><strong>hexbin</strong></span>といったメソッドがあります。それぞれの使い方を以降で解説します。</p>



<h3 class="wp-block-heading jinr-heading d--bold" id="histによるヒストグラムの描画">histによるヒストグラムの描画</h3>



<h4 class="wp-block-heading jinr-heading d--bold" id="基本的な使い方">基本的な使い方</h4>



<p><span class="marker"><strong>hist</strong></span>メソッドを用いてヒストグラムを表示する場合には、以下の例のように使用します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np
import matplotlib.pyplot as plt

plt.style.use('seaborn-whitegrid')

# 正規分布に従うデータの生成
d = np.random.randn(1000)

# ヒストグラムの表示
plt.hist(d)

plt.show()</pre>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img fetchpriority="high" decoding="async" width="642" height="546" src="https://tech.nkhn37.net/wp-content/uploads/2021/10/image-5.png" alt="matplotlib hist ヒストグラム" class="wp-image-1945" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/10/image-5.png 642w, https://tech.nkhn37.net/wp-content/uploads/2021/10/image-5-300x255.png 300w" sizes="(max-width: 642px) 100vw, 642px" /></figure>
</div>


<p>この例では、標準正規分布に従うデータ点をnp.random.randnメソッドで1,000点用意して、当該データに対するヒストグラムを表示しています。以降正規分布をベースに紹介しますが、データの種類はどういった入力でも構いません。</p>



<h4 class="wp-block-heading jinr-heading d--bold" id="ヒストグラムのカスタマイズ">ヒストグラムのカスタマイズ</h4>



<p>一般的な使い方では、各値の階級（これをビン(bins)と言います。）の数などは自動で設定されます。これらの値を設定したい場合には、histの引数として以下の例のように各値を入れることでヒストグラムの描画の見た目を変えることができます。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np
import matplotlib.pyplot as plt

plt.style.use('seaborn-whitegrid')

# 正規分布に従うデータの生成
d = np.random.randn(1000)

# ヒストグラムの表示（各種設定）
plt.hist(d, bins=30, color='red', edgecolor='k', alpha=0.5)

plt.show()</pre>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="642" height="546" src="https://tech.nkhn37.net/wp-content/uploads/2021/10/image-6.png" alt="matplotlib hist ヒストグラム パラメータ" class="wp-image-1946" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/10/image-6.png 642w, https://tech.nkhn37.net/wp-content/uploads/2021/10/image-6-300x255.png 300w" sizes="(max-width: 642px) 100vw, 642px" /></figure>
</div>


<p>この例では、階級（bins）の数を30と設定し、色（color）を赤、枠線（edgecolor）を黒、色の透過度（alpha）を0.5として表示してみています。色々なパラメータについては、histメソッドのドキュメントを確認してみてください。</p>



<section class="wp-block-jinr-blocks-iconbox b--jinr-block b--jinr-iconbox"><div class="d--simple-iconbox6 ">
			<i class="jif jin-ifont-v2books" aria-hidden="true"></i>
			<div class="a--jinr-iconbox">
<p><code>hist</code> の公式ドキュメントは<a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html" target="_blank" rel="noreferrer noopener">こちら</a>を参照してください。</p>
</div>
		</div></section>



<h4 class="wp-block-heading jinr-heading d--bold" id="複数のヒストグラムを同時に描画">複数のヒストグラムを同時に描画</h4>



<p>色々なデータの分析をしていると、複数のデータセットを重ねて描画して、各分布の違いを比較したい場合があります。複数のヒストグラムを同時に描画したい場合には以下の例のように実行することができます。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np
import matplotlib.pyplot as plt

plt.style.use('seaborn-whitegrid')

# 正規分布に従うデータを複数生成
# 平均0, 標準偏差1
d1 = np.random.normal(0, 1, 1000)
# 平均-2, 標準偏差0.5
d2 = np.random.normal(-2, 0.5, 1000)
# 平均5, 標準偏差3
d3 = np.random.normal(5, 3, 1000)

# 設定値の作成
kwargs = dict(bins=30, alpha=0.5, histtype='stepfilled', edgecolor='k')

# ヒストグラムの表示
plt.hist(d1, **kwargs)
plt.hist(d2, **kwargs)
plt.hist(d3, **kwargs)

plt.show()</pre>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="642" height="546" src="https://tech.nkhn37.net/wp-content/uploads/2021/10/image-7.png" alt="matplotlib hist 複数ヒストグラム 重ねる" class="wp-image-1947" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/10/image-7.png 642w, https://tech.nkhn37.net/wp-content/uploads/2021/10/image-7-300x255.png 300w" sizes="(max-width: 642px) 100vw, 642px" /></figure>
</div>


<p>この例では、平均や標準偏差が異なるランダムなデータを生成して各ヒストグラムを表示しています。</p>



<p>結局やっていることは連続でhistメソッドを実行しているだけです。ただ、透過率（alpha）を指定しないと各グラフが見えなくなってしまうため、透過度を設定して各グラフの重なりが分かるようにしています。</p>



<p>設定値を毎回同じような記載するのが面倒なのでkwargsという辞書にまとめて、histメソッドの引数として渡しています。各ヒストグラムでそれぞれ見た目を変えたい場合は、histメソッドにそれぞれパラメータを設定してください。</p>



<h3 class="wp-block-heading jinr-heading d--bold" id="hist2dによる2次元ヒストグラムの描画">hist2dによる2次元ヒストグラムの描画</h3>



<p>2次元のヒストグラムを描画したい場合には、<span class="marker"><strong>hist2d</strong></span>メソッドを使用します。hist2dメソッドを用いて2次元ヒストグラムを表示する場合には、以下の例のように使用します。 </p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np
import matplotlib.pyplot as plt

plt.style.use('seaborn-whitegrid')

# 2次元正規分布の平均と共分散を定義
# 平均
m = [0, 0]
# 共分散
cov = [[1, 1], [1, 2]]
# データの生成
x, y = np.random.multivariate_normal(m, cov, 1000).T

# 2次元ヒストグラムの表示
plt.hist2d(x, y, bins=30, cmap='Purples')
plt.colorbar()

plt.show()</pre>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="642" height="546" src="https://tech.nkhn37.net/wp-content/uploads/2021/10/image-8.png" alt="matplotlib hist2d 二次元ヒストグラム" class="wp-image-1948" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/10/image-8.png 642w, https://tech.nkhn37.net/wp-content/uploads/2021/10/image-8-300x255.png 300w" sizes="(max-width: 642px) 100vw, 642px" /></figure>
</div>


<p>この例では、2次元正規分布に従うデータをnp.random.multivariate_normalメソッドで生成しています。生成するために、平均(m)と共分散(cov)を定義して引数として渡しています。</p>



<p>2次元ヒストグラムは色の濃いところが数が多いことを表していて、イメージとしては画面の手前側にバーが伸びているようなイメージをしてもらえると分かるかと思います。</p>



<section class="wp-block-jinr-blocks-iconbox b--jinr-block b--jinr-iconbox"><div class="d--simple-iconbox6 ">
			<i class="jif jin-ifont-v2books" aria-hidden="true"></i>
			<div class="a--jinr-iconbox">
<p><code>hist2d</code> の公式ドキュメントは<a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist2d.html" target="_blank" rel="noreferrer noopener">こちら</a>を参照してください。</p>
</div>
		</div></section>



<h3 class="wp-block-heading jinr-heading d--bold" id="hexbinによる2次元の六角形ヒストグラム描画">hexbinによる2次元の六角形ヒストグラム描画</h3>



<p>hist2dによる2次元ヒストグラムでは、各グリッドを四角形で表現していました。もう一つの表現方法として各グリッドを六角形で表現する方法があります。六角形で2次元ヒストグラムを表示するには、<span class="marker"><strong>hexbin</strong></span>メソッドを使用します。</p>



<p>hexbinメソッドを用いて2次元ヒストグラムを表示する場合には、以下の例のように使用します。  </p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np
import matplotlib.pyplot as plt

plt.style.use('seaborn-whitegrid')

# 2次元正規分布の平均と共分散を定義
# 平均
m = [0, 0]
# 共分散
cov = [[1, 1], [1, 2]]
# データの生成
x, y = np.random.multivariate_normal(m, cov, 1000).T

# 六角形での2次元ヒストグラムの表示
plt.hexbin(x, y, gridsize=30, cmap='Purples')
plt.colorbar()

plt.show()</pre>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="642" height="546" src="https://tech.nkhn37.net/wp-content/uploads/2021/10/image-9.png" alt="matplotlib hexbin 二次元ヒストグラム 六角形" class="wp-image-1950" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/10/image-9.png 642w, https://tech.nkhn37.net/wp-content/uploads/2021/10/image-9-300x255.png 300w" sizes="(max-width: 642px) 100vw, 642px" /></figure>
</div>


<section class="wp-block-jinr-blocks-iconbox b--jinr-block b--jinr-iconbox"><div class="d--simple-iconbox6 ">
			<i class="jif jin-ifont-v2books" aria-hidden="true"></i>
			<div class="a--jinr-iconbox">
<p><code>hexbin</code> の公式ドキュメントは<a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hexbin.html" target="_blank" rel="noreferrer noopener">こちら</a>を参照してください。</p>
</div>
		</div></section>



<section class="wp-block-jinr-blocks-simplebox b--jinr-block-container"><div class="b--jinr-block b--jinr-box d--heading-box8  "><div class="a--simple-box-title d--bold">ソースコード</div><div class="c--simple-box-inner">
<p>上記で紹介しているソースコードについては <a href="https://github.com/nkhn37/python-tech-sample-source/tree/main/python-data-analysis/matplotlib/hist" target="_blank" rel="noreferrer noopener">GitHub</a> にて公開しています。参考にしていただければと思います。</p>
</div></div></section>


<section class="b--jinr-block b--jinr-blogcard d--blogcard-hover-up d--blogcard-style1 d--blogcard-mysite t--round "><div class="a--blogcard-label ef">あわせて読みたい</div><a class="o--blogcard-link t--round" href="https://tech.nkhn37.net/python-tech-summary-page/"><div class="c--blogcard-image"><img decoding="async" class="a--blogcard-img-src" width="128" height="72" src="https://tech.nkhn37.net/wp-content/uploads/2024/08/Python-Tech-Pythonプログラミングガイド_new1-640x360.jpg" alt="【Python Tech】プログラミングガイド" /></div><div class="a--blogcard-title d--bold">【Python Tech】プログラミングガイド</div></a></section>

]]></content:encoded>
					
					<wfw:commentRss>https://tech.nkhn37.net/matplotlib-hist-hist2d-hexbin/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Disk: Enhanced  を使用したページ キャッシュ

Served from: tech.nkhn37.net @ 2026-05-21 09:24:56 by W3 Total Cache
-->