<?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>「use」タグの記事一覧Python Tech</title>
	<atom:link href="https://tech.nkhn37.net/tag/use/feed/" rel="self" type="application/rss+xml" />
	<link>https://tech.nkhn37.net</link>
	<description>Python学習サイト</description>
	<lastBuildDate>Mon, 12 Jan 2026 21:05:38 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://tech.nkhn37.net/wp-content/uploads/2021/01/cropped-lion-normal-clear-1-32x32.png</url>
	<title>「use」タグの記事一覧Python Tech</title>
	<link>https://tech.nkhn37.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【matplotlib】基本的な使用方法</title>
		<link>https://tech.nkhn37.net/matplotlib-basic-usage/</link>
					<comments>https://tech.nkhn37.net/matplotlib-basic-usage/#respond</comments>
		
		<dc:creator><![CDATA[naoki-hn]]></dc:creator>
		<pubDate>Sun, 19 Sep 2021 00:00:00 +0000</pubDate>
				<category><![CDATA[matplotlib]]></category>
		<category><![CDATA[%matplotlib]]></category>
		<category><![CDATA[%matplotlib inline]]></category>
		<category><![CDATA[plot]]></category>
		<category><![CDATA[savefig]]></category>
		<category><![CDATA[show]]></category>
		<category><![CDATA[use]]></category>
		<guid isPermaLink="false">https://tech.nkhn37.net/?p=1850</guid>

					<description><![CDATA[Pythonでデータ分析をする際にグラフなどの可視化でよく利用されるmatplotlibの基本的な使用方法について解説します。 matplotlibによるデータの可視化 データ分析では、対象データや解析した結果を可視化す [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Pythonでデータ分析をする際にグラフなどの可視化でよく利用される<span class="marker"><strong>matplotlibの基本的な使用方法</strong></span>について解説します。</p>



<h2 class="wp-block-heading jinr-heading d--bold" id="matplotlibのインストール">matplotlibによるデータの可視化</h2>



<p class="wp-block-paragraph">データ分析では、対象データや解析した結果を可視化するということが必ずと言っていいほど出てきます。Pythonのデータ可視化のパッケージとして有名なものが<span class="marker"><strong>matplotlib</strong></span>です。</p>



<p class="wp-block-paragraph">pythonでのデータ分析では、numpy、pandas、matplotlib、scikit-learnがセットで出てくることが多いかと思います（他にも有用なパッケージは沢山あります）。データ可視化手法としてmatplotlibの使い方を覚えていくことは有効です。</p>



<p class="wp-block-paragraph">本記事では、matplotlibの基本的な使い方について紹介します。</p>



<h2 class="wp-block-heading jinr-heading d--bold">matplotlibのインストール</h2>



<p class="wp-block-paragraph"><span class="marker"><strong>matplotlib</strong></span>を使用するためにはパッケージのインストールが必要です。matplotlibがインストールされていない場合は、以下のpipコマンドを使ってインストールしてください。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install matplotlib</pre>



<p class="wp-block-paragraph"></p>



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



<p class="wp-block-paragraph">以降では、matplotlibを使った簡単なデータ可視化について解説します。</p>



<h3 class="wp-block-heading jinr-heading d--bold" id="matplotlibのインポート">matplotlibのインポート</h3>



<p class="wp-block-paragraph">matplotlibを使用するには、まずmatplotlibをimportします。matplotlibを使用する場合は、インポートする際にmplやpltのような省略形として使うのが一般的となっています。</p>



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



<p class="wp-block-paragraph">以降で紹介するコードは上記のようにimportがされていることを前提とします。</p>



<h3 class="wp-block-heading jinr-heading d--bold" id="スタイルの設定">スタイル設定</h3>



<p class="wp-block-paragraph">matplotlibでは、描画スタイルを設定することができます。スタイル設定をするには<span class="marker"><strong>style.use</strong></span>メソッドを使用します。</p>



<p class="wp-block-paragraph">以下は&#8217;classic&#8217;スタイルに設定する場合の設定方法です。この設定をすることでグラフの表示スタイルを変更することができます。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">plt.style.use("classic")</pre>



<p class="wp-block-paragraph">この設定をしなくてもmatplotlibは利用できますが、自分の目的に沿ったスタイルを設定して表示できることを覚えておくとよいでしょう。</p>



<p class="wp-block-paragraph">また、使用できるスタイルを確認するには以下のようにstyle.availableを表示すること確認できます。なお、以下の例はprint関数を使用していますが、IPythonのコンソールやJupyter Notebookを使用している場合は、シンプルに「plt.style.available」を実行するだけで確認ができます。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(plt.style.available)</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">【実行結果例】
['Solarize_Light2', '_classic_test_patch', '_mpl-gallery', '_mpl-gallery-nogrid', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-v0_8', 'seaborn-v0_8-bright', 'seaborn-v0_8-colorblind', 'seaborn-v0_8-dark', 'seaborn-v0_8-dark-palette', 'seaborn-v0_8-darkgrid', 'seaborn-v0_8-deep', 'seaborn-v0_8-muted', 'seaborn-v0_8-notebook', 'seaborn-v0_8-paper', 'seaborn-v0_8-pastel', 'seaborn-v0_8-poster', 'seaborn-v0_8-talk', 'seaborn-v0_8-ticks', 'seaborn-v0_8-white', 'seaborn-v0_8-whitegrid', 'tableau-colorblind10']</pre>



<h3 class="wp-block-heading jinr-heading d--bold" id="matplotlibでのプロット方法">matplotlibでの描画方法</h3>



<h4 class="wp-block-heading jinr-heading d--bold" id="pythonスクリプトでプロットする場合">Pythonスクリプトで描画する場合</h4>



<p class="wp-block-paragraph">Pythonのスクリプト内でmatplotlibを用いた描画をする場合には、以下の例のように使用します。</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 matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))

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/09/image.png" alt="matplotlib plot sin cos" class="wp-image-1852" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/09/image.png 642w, https://tech.nkhn37.net/wp-content/uploads/2021/09/image-300x255.png 300w" sizes="(max-width: 642px) 100vw, 642px" /></figure>
</div>


<p class="wp-block-paragraph">上記の例では、numpyのlinspaceで横軸であるxのデータを作成し、sin(x)およびcos(x)を表示しています。</p>



<p class="wp-block-paragraph">スクリプトで実際に画面描画する場合には、<span class="marker"><strong>show</strong></span>メソッドを実行する必要があります。「plt.show()」というコマンドを忘れずに記述するようにしましょう。</p>



<h4 class="wp-block-heading jinr-heading d--bold" id="ipythonのコンソールで実行する場合">IPythonのコンソールで描画する場合</h4>



<p class="wp-block-paragraph">IPythonのコンソールで実行する場合には以下のように「<span class="marker"><strong>%matplotlib</strong></span>」というマジックコマンドを使用することでmatplotlibの描画をすることができます。実行している内容は上記の例と同じsin(x), cos(x)の描画です。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="850" height="528" src="https://tech.nkhn37.net/wp-content/uploads/2021/09/image-3.png" alt="matplotlib IPython" class="wp-image-1855" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/09/image-3.png 850w, https://tech.nkhn37.net/wp-content/uploads/2021/09/image-3-300x186.png 300w, https://tech.nkhn37.net/wp-content/uploads/2021/09/image-3-768x477.png 768w" sizes="(max-width: 850px) 100vw, 850px" /></figure>
</div>


<p class="wp-block-paragraph">IPythonで実行する場合には、plotのたびにFigureに追記されていきます。</p>



<p class="wp-block-paragraph">【plt.plot(x, np.sin(x))の実行後】</p>


<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/09/image-1.png" alt="matplotlib plot sin IPython" class="wp-image-1853" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/09/image-1.png 642w, https://tech.nkhn37.net/wp-content/uploads/2021/09/image-1-300x255.png 300w" sizes="(max-width: 642px) 100vw, 642px" /></figure>
</div>


<p class="wp-block-paragraph">【plt.plot(x, np.cos(x))の実行後】</p>


<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/09/image-2.png" alt="matplotlib plot sin cos IPython" class="wp-image-1854" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/09/image-2.png 642w, https://tech.nkhn37.net/wp-content/uploads/2021/09/image-2-300x255.png 300w" sizes="(max-width: 642px) 100vw, 642px" /></figure>
</div>


<p class="wp-block-paragraph">IPythonやマジックコマンドの概要についてご存じない方は「<a href="https://tech.nkhn37.net/python-ipython-basic/" target="_blank" rel="noreferrer noopener">IPython ~拡張された対話型インタプリタ~</a>」を参考にしていただければと思います。</p>



<h4 class="wp-block-heading jinr-heading d--bold" id="jupyter-notebookで実行する場合">Jupyter Notebookで描画する場合</h4>



<p class="wp-block-paragraph">numpyやpandas等を利用する多くの方が実行環境としてJupyter Notebookをしているかと思います。Jupyter Notebookでのプロットはとても簡単です。</p>



<p class="wp-block-paragraph">Jupyter Notebookでインラインで画像を表示するためには「<span class="marker"><strong>%matplotlib inline</strong></span>」というマジックコマンドを含めます。これにより、インラインで画像化したグラフを埋め込んで表示してくれます。  実行している内容は上記の例と同じsin(x), cos(x)の描画です。 </p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="742" height="471" src="https://tech.nkhn37.net/wp-content/uploads/2021/09/image-6.png" alt="matplotlib sin cos Jupyter Notebook" class="wp-image-1858" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/09/image-6.png 742w, https://tech.nkhn37.net/wp-content/uploads/2021/09/image-6-300x190.png 300w" sizes="(max-width: 742px) 100vw, 742px" /></figure>
</div>


<p class="wp-block-paragraph">Jupyter Notebookの基本的な使用方法を確認したい場合は「<a href="https://tech.nkhn37.net/python-jupyter-notebook-basic/" target="_blank" rel="noreferrer noopener">Jupyter Notebookの基本的な使い方</a>」を参考にしていただければと思います。</p>



<h3 class="wp-block-heading" id="画像を保存する方法">描画したグラフを画像として保存する方法</h3>



<p class="wp-block-paragraph">スクリプトから画像として保存する場合には以下のように「<span class="marker"><strong>savefig</strong></span>」メソッドを使用します。</p>



<p class="wp-block-paragraph">メソッドを使用するために「fig = plt.figure()」というコードでFigureクラスのインスタンスを用意している点が上記の例とは違います。</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 matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

fig = plt.figure()
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))

fig.savefig("temp.png")

plt.show()</pre>



<p class="wp-block-paragraph">なお、保存する画像の種類はファイル名の拡張子から自動で判別して保存してくれます。使用している環境で使用できる拡張子は以下で確認することができます。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(fig.canvas.get_supported_filetypes())</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{'eps': 'Encapsulated Postscript', 'jpg': 'Joint Photographic Experts Group', 'jpeg': 'Joint Photographic Experts Group', 'pdf': 'Portable Document Format', 'pgf': 'PGF code for LaTeX', 'png': 'Portable Network Graphics', 'ps': 'Postscript', 'raw': 'Raw RGBA bitmap', 'rgba': 'Raw RGBA bitmap', 'svg': 'Scalable Vector Graphics', 'svgz': 'Scalable Vector Graphics', 'tif': 'Tagged Image File Format', 'tiff': 'Tagged Image File Format', 'webp': 'WebP Image Format'}</pre>



<p class="wp-block-paragraph">IPythonのコンソールやJupyter Notebookを使用している場合は、シンプルに「fig.canvas.get_supported_filetypes()」を実行するだけで確認することができます。 </p>



<p class="wp-block-paragraph">また、もちろんですが表示されているFigureの保存ボタンからファイル名を指定して保存することも可能です。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="655" height="558" src="https://tech.nkhn37.net/wp-content/uploads/2021/09/image-7.png" alt="matplotlib savefig" class="wp-image-1859" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/09/image-7.png 655w, https://tech.nkhn37.net/wp-content/uploads/2021/09/image-7-300x256.png 300w" sizes="(max-width: 655px) 100vw, 655px" /></figure>
</div>


<h2 class="wp-block-heading jinr-heading d--bold">まとめ</h2>



<p class="wp-block-paragraph">Pythonでデータ分析をする際にグラフなどの可視化でよく利用される<span class="marker"><strong>matplotlibの基本的な使用方法</strong></span>について解説しました。</p>



<p class="wp-block-paragraph">matplotlibは、numpyやpandas、scikit-learn等と共にデータ分析でよく活用される可視化ライブラリです。是非、使い方を色々と覚えてもらえるとよいかと思います。</p>



<p class="wp-block-paragraph">matplotlib関連は他にも以下のようなページをまとめています。興味がある項目があれば参考にしていただければと思います。</p>



<ul class="wp-block-list jinr-list">
<li><a href="https://tech.nkhn37.net/matplotlib-subplots/" target="_blank" rel="noreferrer noopener">複数グラフを一つのウィンドウに表示する方法</a></li>



<li><a href="https://tech.nkhn37.net/matplotlib-color-linestyle/" target="_blank" rel="noreferrer noopener">線の色や形状を変更する方法</a></li>



<li><a href="https://tech.nkhn37.net/matplotlib-plot-lim-axis/" target="_blank" rel="noreferrer noopener">グラフの軸の範囲の設定方法</a></li>



<li><a href="https://tech.nkhn37.net/matplotlib-title-label-legend-set/" target="_blank" rel="noreferrer noopener">タイトル・軸名・凡例の設定方法</a></li>



<li><a href="https://tech.nkhn37.net/matplotlib-plot-scatter/" target="_blank" rel="noreferrer noopener">散布図の描画方法 ~ plot, scatter ~</a></li>



<li><a href="https://tech.nkhn37.net/matplotlib-hist-hist2d-hexbin/" target="_blank" rel="noreferrer noopener">ヒストグラムの描画方法 ~ hist, hist2d, hexbin ~</a></li>



<li><a href="https://tech.nkhn37.net/matplotlib-contour-contourf/" target="_blank" rel="noreferrer noopener">等高線プロットの描画方法 ~ contour, contourf ~</a></li>



<li><a href="https://tech.nkhn37.net/matplotlib-errorbar-basic/" target="_blank" rel="noreferrer noopener">エラーバーの描画方法 ~ errorbar ~</a></li>
</ul>



<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 class="wp-block-paragraph"><code>matplotlib</code> の公式ドキュメントは<a href="https://matplotlib.org/stable/users/index.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 class="wp-block-paragraph">上記で紹介しているソースコードについては <a href="https://github.com/nkhn37/python-tech-sample-source/tree/main/python-data-analysis/matplotlib/basic" 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>




<p class="wp-block-paragraph"></p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.nkhn37.net/matplotlib-basic-usage/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-07-04 09:30:07 by W3 Total Cache
-->