{"id":2592,"date":"2025-10-25T19:07:33","date_gmt":"2025-10-25T17:07:33","guid":{"rendered":"https:\/\/bioinfo2.ugr.es\/biocomputacion\/?page_id=2592"},"modified":"2025-10-25T19:28:44","modified_gmt":"2025-10-25T17:28:44","slug":"graficos","status":"publish","type":"page","link":"https:\/\/bioinfo2.ugr.es\/biocomputacion\/graficos\/","title":{"rendered":"Gr\u00e1ficos"},"content":{"rendered":"<p>Los DataFrames juegan un papel importante a la hora de generar gr\u00e1ficos. Tienen un m\u00e9todo plot() para generar gr\u00e1ficos directamente<\/p>\n<p><span style=\"font-weight: 400;\">df.plot(<\/span><span style=\"font-weight: 400;\">kind=<\/span><span style=\"font-weight: 400;\"> argument<\/span><span style=\"font-weight: 400;\">)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Tipos<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Kind<\/b><\/td>\n<td><b>Function<\/b><\/td>\n<td><b>Typical Use<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8216;line&#8217;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">df.plot.line()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Trends, time series<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8216;bar&#8217;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">df.plot.bar()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Categorical comparisons<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8216;barh&#8217;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">df.plot.barh()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Horizontal bar chart<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8216;hist&#8217;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">df.plot.hist()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Distribution of values<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8216;box&#8217;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">df.plot.box()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Statistical summary (min, Q1, median, etc.)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8216;kde&#8217;<\/span><span style=\"font-weight: 400;\"> \/ <\/span><span style=\"font-weight: 400;\">&#8216;density&#8217;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">df.plot.kde()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Smoothed density curve<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8216;area&#8217;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">df.plot.area()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Cumulative values or composition<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8216;pie&#8217;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">df.plot.pie()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Proportions of categories<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8216;scatter&#8217;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">df.plot.scatter(x=&#8217;col1&#8242;, y=&#8217;col2&#8242;)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Relationship between two variables<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8216;hexbin&#8217;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">df.plot.hexbin(x=&#8217;col1&#8242;, y=&#8217;col2&#8242;, gridsize=25)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Density of 2D points<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4>Visualizar las frecuencias de codones:<\/h4>\n<p>Gr\u00e1fico de baras:<\/p>\n<pre><span style=\"font-weight: 400;\">import<\/span><span style=\"font-weight: 400;\"> pandas as pd<\/span>\r\n<span style=\"font-weight: 400;\">import<\/span><span style=\"font-weight: 400;\"> matplotlib.pyplot <\/span><span style=\"font-weight: 400;\">as<\/span><span style=\"font-weight: 400;\"> plt<\/span>\r\n\r\ninfile='\/opt\/datos\/plotting_25\/thermophilus_codon.tsv'\r\ndf = pd.read_csv(infile,sep='\\t') # df es un DataFrame\r\ndf.plot(kind='bar',x='Codon',y='Frequency',xlabel='Codon',ylabel='Frequency (per 1k)',title=\"Codon Frequency of Thermus thermophilus \")\r\nplt.show()\r\n\r\n<\/pre>\n<pre><span style=\"font-weight: 400;\">Para guardar el gr\u00e1fico\r\ndf.plot()<\/span> \r\n<span style=\"font-weight: 400;\">ax = df.plot()<\/span>\r\n<span style=\"font-weight: 400;\">plt.savefig(\"plot1.png\")<\/span>\r\n<span style=\"font-weight: 400;\">plt.close()<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h4>\u00bfComo podemos graficar la frecuencia para cada amino\u00e1cido?<\/h4>\n<p>Soluci\u00f3n: los m\u00e9todos groupby() y agg()<\/p>\n<div>\n<div><\/div>\n<pre>grouped_df = df.groupby('AA',as_index=False).agg(Frequency=('Frequency',\"sum\"))\r\n\r\nprint(grouped_df.head())\r\n\r\ngrouped_df['sample']='thermo'<\/pre>\n<\/div>\n<h4>Usar plotly<\/h4>\n<div>\n<div>\n<div>\n<pre>import plotly.express as px<\/pre>\n<\/div>\n<pre>fig = px.bar(grouped_df,x='AA',y='Frequency')\r\nfig.show()\r\nexit()<\/pre>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<h4>Comparar datos<\/h4>\n<h5>Tarea1:<\/h5>\n<p>Tenemos otra salida del cusp: \/opt\/datos\/plotting_25\/NC_007633_codon.tsv<\/p>\n<p>Generar un gr\u00e1fico comparando las frecuencias de los dos ficheros<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Los DataFrames juegan un papel importante a la hora de generar gr\u00e1ficos. Tienen un m\u00e9todo plot() para generar gr\u00e1ficos directamente df.plot(kind= argument) Tipos Kind Function Typical Use &#8216;line&#8217; df.plot.line() Trends, time series &#8216;bar&#8217; df.plot.bar() Categorical comparisons &#8216;barh&#8217; df.plot.barh() Horizontal bar <a href=\"https:\/\/bioinfo2.ugr.es\/biocomputacion\/graficos\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/bioinfo2.ugr.es\/biocomputacion\/wp-json\/wp\/v2\/pages\/2592"}],"collection":[{"href":"https:\/\/bioinfo2.ugr.es\/biocomputacion\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bioinfo2.ugr.es\/biocomputacion\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bioinfo2.ugr.es\/biocomputacion\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bioinfo2.ugr.es\/biocomputacion\/wp-json\/wp\/v2\/comments?post=2592"}],"version-history":[{"count":7,"href":"https:\/\/bioinfo2.ugr.es\/biocomputacion\/wp-json\/wp\/v2\/pages\/2592\/revisions"}],"predecessor-version":[{"id":2599,"href":"https:\/\/bioinfo2.ugr.es\/biocomputacion\/wp-json\/wp\/v2\/pages\/2592\/revisions\/2599"}],"wp:attachment":[{"href":"https:\/\/bioinfo2.ugr.es\/biocomputacion\/wp-json\/wp\/v2\/media?parent=2592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}