{"id":52,"date":"2012-04-26T13:32:36","date_gmt":"2012-04-26T13:32:36","guid":{"rendered":"http:\/\/azfreeos.wordpress.com\/?p=52"},"modified":"2013-09-27T14:32:39","modified_gmt":"2013-09-27T14:32:39","slug":"plot-1-d-graph-using-python","status":"publish","type":"post","link":"https:\/\/dosen.unila.ac.id\/ahmadzakaria\/2012\/04\/26\/plot-1-d-graph-using-python\/","title":{"rendered":"Plot 1-D Graph using Python"},"content":{"rendered":"<p>Drawing graphs or plotting graphics in one dimension (x-y), we usually do by using a program such as excel Worksheet of the Spreadsheet from Microsoft Excel, Open Office, Gnumeric, or Matlab, and many more programs that can be used for this purpose. But when the data series is very long perhaps Excel Spreadsheet, Gnumeric, and OpenOfice were not able help us to draw a graph.<\/p>\n<hr \/>\n<p>For it then we can only use other programs such as Matlab, GnuPlot, Octplot, Octave, Python, and so on.<\/p>\n<p>Here, I am still learning this and try to share my experience of making or drawing a graph (x-y) signals from sound waves using Python.<\/p>\n<p>The data series will be read data signals with the name &#8220;signals.inp&#8221; in ASCII such as format as follows,<\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013 file signals.txt \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013<\/p>\n<div lang=\"en\">\n<p>10000<\/p>\n<\/div>\n<div lang=\"en\">\n<p>0,-189<\/p>\n<\/div>\n<div lang=\"en\">\n<p>1.04166666666667e-05,-209<\/p>\n<\/div>\n<div lang=\"en\">\n<p>2.08333333333333e-05,-201<\/p>\n<\/div>\n<div lang=\"en\">\n<p>3.125e-05,-195<\/p>\n<\/div>\n<div lang=\"en\">\n<p>4.16666666666667e-05,-195<\/p>\n<\/div>\n<div lang=\"en\">\n<p>5.20833333333333e-05,-202<\/p>\n<\/div>\n<div lang=\"en\">\n<p>6.25e-05,-218<\/p>\n<\/div>\n<div lang=\"en\">\n<p>7.29166666666667e-05,-222<\/p>\n<\/div>\n<div lang=\"en\">\n<p>8.33333333333333e-05,-223<\/p>\n<\/div>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013 file signals.txt \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013<\/p>\n<p>The length of this file is 10,000 (ten thousand) line, in 2 (two) coma-separated fields.<\/p>\n<p>This File can be read with the python script (&#8220;baca_signals. py&#8221;) as follows,<\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013 file baca_signals.py \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013<\/p>\n<p>#!\/usr\/bin\/env python<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>from numpy import *<\/p>\n<p>b = []<\/p>\n<p>data = open(\u201dsignals.txt\u201d,\u201dr\u201d)<\/p>\n<p>m = data.readline()<\/p>\n<p>baris = data.readlines()<\/p>\n<p>for i in range(int(m)):<\/p>\n<p>kolom = baris[i].split(\u201d,\u201d)<\/p>\n<p>a = list(kolom)<\/p>\n<p>b = b + a<\/p>\n<p>c = list(b)<\/p>\n<p>d = reshape(array(c),(float(m),2))<\/p>\n<p>d1 = take(d,(0,),1)<\/p>\n<p>d2 = take(d,(1,),1)<\/p>\n<p>plt.plot(d1,d2)<\/p>\n<p>plt.xlabel(\u2019time (msec)\u2019,fontsize=20)<\/p>\n<p>plt.ylabel(\u2019intensity\u2019,fontsize=20)<\/p>\n<p>plt.xticks(fontsize=20)<\/p>\n<p>plt.yticks(fontsize=20)<\/p>\n<p>plt.axis([0,0.11,-6000,6000])<\/p>\n<p>plt.title(\u2019sound waves\u2019,fontsize=25)<\/p>\n<p>plt.grid(\u2019true\u2019)<\/p>\n<p>plt.show()<\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013 file baca_signals.py \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013<\/p>\n<p>Python Script above is a script that used to read the file &#8220;signals .txt&#8221;.<\/p>\n<p>This Script can be run with the command as follows,<\/p>\n<p>abuadnan@abuadnan-laptop:~\/Documents$ .\/<strong>baca_signals.py<\/strong><\/p>\n<p>Then, it will generate an image as follows,<\/p>\n<p><a href=\"https:\/\/dosen.unila.ac.id\/wp-content\/uploads\/sites\/21\/2012\/04\/plot1d_python.jpg\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter\" alt=\"\" src=\"https:\/\/dosen.unila.ac.id\/wp-content\/uploads\/sites\/21\/2012\/04\/plot1d_python.jpg?w=300\" width=\"320\" height=\"179\" border=\"0\" \/><\/a><\/p>\n<div align=\"CENTER\">\n<p>Figure 1. The Plot of signal sound waves.<\/p>\n<\/div>\n<p>Figure 1 above, the image signal can be stored. When the save icon in click then will appear the following image,<\/p>\n<p><a href=\"https:\/\/dosen.unila.ac.id\/wp-content\/uploads\/sites\/21\/2012\/04\/plot1d_py_save.jpg\"><img decoding=\"async\" class=\"aligncenter\" alt=\"\" src=\"https:\/\/dosen.unila.ac.id\/wp-content\/uploads\/sites\/21\/2012\/04\/plot1d_py_save.jpg?w=300\" width=\"320\" height=\"179\" border=\"0\" \/><\/a><\/p>\n<div>\n<p style=\"text-align: center\">Figure 2. Save the image into a file *. png<\/p>\n<\/div>\n<div align=\"LEFT\">\u00a0After that it will appear with the image file name and format in accordance with the name and format choices we wish. See the file results in the following,<\/div>\n<p style=\"text-align: center\"><a href=\"http:\/\/azfreeos.files.wordpress.com\/2012\/04\/plot1d_py_signals.png\"><img decoding=\"async\" class=\"aligncenter\" alt=\"\" src=\"http:\/\/azfreeos.files.wordpress.com\/2012\/04\/plot1d_py_signals.png?w=300\" width=\"320\" height=\"153\" border=\"0\" \/><\/a>Figure 3. The Image File that is already stored and saved.<\/p>\n<div><\/div>\n<div><\/div>\n<div lang=\"en\">\n<p>Figure 3 is a plot of signal sound waves that are read from the data file, drawn and saved in the image file format (in this case in &#8220;png&#8221; format).<\/p>\n<\/div>\n<div lang=\"en\"><\/div>\n<div lang=\"en\">\u00a0So, hopefully this is helpful to all users of linux or windows that use Python as a free program (free softwares). This example File can be downloaded from <a href=\"https:\/\/dosen.unila.ac.id\/wp-content\/uploads\/sites\/21\/2013\/09\/baca1d-python.zip\" target=\"_blank\">here<\/a> (50,8 Kb)<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Drawing graphs or plotting graphics in one dimension (x-y), we usually do by using a program such as excel Worksheet of the Spreadsheet from Microsoft Excel, Open Office, Gnumeric, or &hellip; <a class=\"readmore\" href=\"https:\/\/dosen.unila.ac.id\/ahmadzakaria\/2012\/04\/26\/plot-1-d-graph-using-python\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":21,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/dosen.unila.ac.id\/ahmadzakaria\/wp-json\/wp\/v2\/posts\/52"}],"collection":[{"href":"https:\/\/dosen.unila.ac.id\/ahmadzakaria\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dosen.unila.ac.id\/ahmadzakaria\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dosen.unila.ac.id\/ahmadzakaria\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/dosen.unila.ac.id\/ahmadzakaria\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":0,"href":"https:\/\/dosen.unila.ac.id\/ahmadzakaria\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"wp:attachment":[{"href":"https:\/\/dosen.unila.ac.id\/ahmadzakaria\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dosen.unila.ac.id\/ahmadzakaria\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dosen.unila.ac.id\/ahmadzakaria\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}