{"id":3356,"date":"2023-02-13T20:00:00","date_gmt":"2023-02-13T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=3356"},"modified":"2023-02-12T23:11:21","modified_gmt":"2023-02-12T14:11:21","slug":"matplotlib-csv","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/matplotlib-csv\/","title":{"rendered":"[matplotlib]How to Plot Data from CSV file[csv module, pandas]"},"content":{"rendered":"\n<p>This article shows <span class=\"st-mymarker-s\">how to plot data from CSV file<\/span>.<\/p>\n\n\n\n<div class=\"wp-block-st-blocks-midashi-box freebox has-title\" style=\"background-color:#eceff1;border-color:#263238;border-radius:0 5px 5px 5px\"><p class=\"p-free\" style=\"border-color:#263238;font-weight:bold\"><span class=\"p-entry-f\" style=\"color:#ffffff;font-weight:bold;background-color:#263238;border-radius:0 0 5px 0\"><i class=\"st-fa st-svg-file-text-o st-css-no\" aria-hidden=\"\"><\/i>Contents<\/span><\/p><div class=\"free-inbox\">\n<p>\u25c6How to plot data from CSV file<\/p>\n\n\n\n<ul><li>Using matplotlib and csv module<\/li><li>Using matplotlib and pandas<\/li><\/ul>\n\n\n\n<p>\u25c6[Supplement]How to plot data from CSV file by using pandas<\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is the csv file of example code.<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib_csv1.jpg\" alt=\"\" class=\"wp-image-3962\" width=\"164\" height=\"277\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib_csv1.jpg 218w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib_csv1-177x300.jpg 177w\" sizes=\"(max-width: 164px) 100vw, 164px\" \/><\/figure><\/div>\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How to plot data from CSV file by using matplotlib and csv module<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to plot data from CSV file by using matplotlib and csv module<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is the example code.<\/p>\n\n\n\n<p>The data in the csv file is read by using the &#8220;csv module&#8221;, a standard python module.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>import csv\nimport numpy as np\nimport matplotlib.pyplot as plt\n\ncsv_path = r'test\\test.csv'\n\nrows = &#091;]\nwith open(csv_path) as f:   \n    reader = csv.reader(f)\n    rows = &#091;row for row in reader]\n\nheader = rows.pop(0)\n\ndata = np.float_(np.array(rows).T)\n\n\nfig, ax = plt.subplots()\n\nax.plot(data&#091;0], data&#091;1], linestyle='solid', marker='o')\n\nax.set_xlabel(header&#091;0])\nax.set_ylabel(header&#091;1])\n\nplt.show()<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The above code generates the following graph.<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"389\" height=\"262\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib_csv2.png\" alt=\"\" class=\"wp-image-3927\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib_csv2.png 389w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib_csv2-300x202.png 300w\" sizes=\"(max-width: 389px) 100vw, 389px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; with open(csv_path) as f:<br>&gt;     reader = csv.reader(f)<br>&gt;     rows = [row for row in reader]<\/p>\n\n\n\n<p>The data in the csv file is read using the &#8220;csv module&#8221;.<\/p>\n\n\n\n<p>The csv.reader( ) is used to read the csv file, which returns an &#8220;reader object&#8221;.<\/p>\n\n\n\n<p>Using comprehension, data is extracted row by row from the &#8220;reader object&#8221; to create a new list.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; header = rows.pop(0)<\/p>\n\n\n\n<p>The 0th element of the csv data is retrieved by using &#8220;pop method&#8221;.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; np.float_(np.array(rows).T)<\/p>\n\n\n\n<p>The data is transformed into a form that can be plotted in a graph.<\/p>\n\n\n\n<p>np.array(rows).T  :  After transforming into numpy type, it is transposed.<\/p>\n\n\n\n<p>np.float_( ) : Transforms to floating type.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax.plot(data[0], data[1], linestyle=&#8217;solid&#8217;, marker=&#8217;o&#8217;)<\/p>\n\n\n\n<p>The data is passed to the graph.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax.set_xlabel(header[0])<br>&gt; ax.set_ylabel(header[1])<\/p>\n\n\n\n<p>X-axis and Y-axis labels are specified.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>How to use of comprehension is explained in this article.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\"><a href=\"https:\/\/python-academia.com\/en\/python-comprehension\/\" data-type=\"URL\" data-id=\"https:\/\/python-academia.com\/en\/python-comprehension\/\">How to Use Comprehension<\/a><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>How to use of &#8220;pop method&#8221; of a list  is explained in this article.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\"><a href=\"https:\/\/python-academia.com\/en\/list-delete\/\">Remove Elements from List [del, remove, pop, clear]<\/a><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">How to plot data from CSV file by using matplotlib and pandas<\/h2>\n\n\n\n<p>Next, I show how to <span class=\"st-mymarker-s\">read csv data by using &#8220;pandas&#8221;<\/span> and <span class=\"st-mymarker-s\">draw a graph by using &#8220;matplotlib&#8221;<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is the example code.<\/p>\n\n\n\n<p>The data in the csv file is read by using &#8220;pandas.read_csv&#8221; function.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>import pandas as pd\nimport matplotlib.pyplot as plt\n\npath_csv = r'test\\test.csv'\ndf_csv = pd.read_csv(path_csv)\n\ndata_x = df_csv&#091;df_csv.columns&#091;0]]\ndata_y = df_csv&#091;df_csv.columns&#091;1]]\n\nfig, ax = plt.subplots()\n\nax.plot(data_x, data_y, linestyle='solid', marker='o')\n\nax.set_xlabel(df_csv.columns&#091;0])\nax.set_ylabel(df_csv.columns&#091;1])\n\nplt.show()<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The above code generates the following graph.<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"389\" height=\"262\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib_csv2.png\" alt=\"\" class=\"wp-image-3927\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib_csv2.png 389w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib_csv2-300x202.png 300w\" sizes=\"(max-width: 389px) 100vw, 389px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; df_csv = pd.read_csv(path_csv)<\/p>\n\n\n\n<p>The data in the csv file is read using the &#8220;pandas.read_csv&#8221; function.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; data_x = df_csv[df_csv.columns[0]]<br>&gt; data_y = df_csv[df_csv.columns[1]]<\/p>\n\n\n\n<p>The data is transformed into a form that can be plotted in a graph.<\/p>\n\n\n\n<p>The &#8220;df_csv.columns&#8221; function is used to get the column names of the DataFrame.<\/p>\n\n\n\n<p>The data corresponding to each of the X and Y axes is extracted by specifying &#8220;df_csv[ &#8216;column name&#8217; ]&#8221;.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-display -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"6239864271\"\n     data-ad-format=\"auto\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">[Supplement]How to plot data from CSV file by using pandas<\/h2>\n\n\n\n<p>As a supplement, I show <span class=\"st-mymarker-s\">how to draw a graph from a csv file by using &#8220;pandas&#8221;<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Pandas also provides a method for drawing graphs.<\/p>\n\n\n\n<p>This method makes it easy to draw graphs.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is the example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>import pandas as pd\n\npath_csv = r'test\\test.csv'\ndf_csv = pd.read_csv(path_csv)\n\nfig = plt.figure()\nax = fig.add_subplot(1, 1, 1)\n\ndf_csv.plot(ax=ax, x=df_csv.columns&#091;0], y=df_csv.columns&#091;1], marker='o')<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The above code generates the following graph.<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"375\" height=\"262\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/pandas_csv1.png\" alt=\"\" class=\"wp-image-3935\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/pandas_csv1.png 375w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/pandas_csv1-300x210.png 300w\" sizes=\"(max-width: 375px) 100vw, 375px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; df_csv.plot(ax=ax, x=df_csv.columns[0], y=df_csv.columns[1], marker=&#8217;o&#8217;)<\/p>\n\n\n\n<p>The &#8220;df.plot( ) method&#8221; is used to draw a graph.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-matched-content-rows-num=\"4,2\"\n     data-matched-content-columns-num=\"1,4\"\n     data-matched-content-ui-type=\"image_stacked,image_stacked\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"2243394422\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>This article shows how to plot data from CSV file. This is the csv file of example code. How to plot &#8230; <\/p>\n","protected":false},"author":1,"featured_media":4001,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6],"tags":[20,21],"_links":{"self":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/3356"}],"collection":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/comments?post=3356"}],"version-history":[{"count":96,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/3356\/revisions"}],"predecessor-version":[{"id":4068,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/3356\/revisions\/4068"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/4001"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=3356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=3356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=3356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}