Los DataFrames juegan un papel importante a la hora de generar gráficos. Tienen un método plot() para generar gráficos directamente
df.plot(kind= argument)
Tipos
| Kind | Function | Typical Use |
| ‘line’ | df.plot.line() | Trends, time series |
| ‘bar’ | df.plot.bar() | Categorical comparisons |
| ‘barh’ | df.plot.barh() | Horizontal bar chart |
| ‘hist’ | df.plot.hist() | Distribution of values |
| ‘box’ | df.plot.box() | Statistical summary (min, Q1, median, etc.) |
| ‘kde’ / ‘density’ | df.plot.kde() | Smoothed density curve |
| ‘area’ | df.plot.area() | Cumulative values or composition |
| ‘pie’ | df.plot.pie() | Proportions of categories |
| ‘scatter’ | df.plot.scatter(x=’col1′, y=’col2′) | Relationship between two variables |
| ‘hexbin’ | df.plot.hexbin(x=’col1′, y=’col2′, gridsize=25) | Density of 2D points |
Visualizar las frecuencias de codones:
Gráfico de baras:
import pandas as pd import matplotlib.pyplot as plt infile='/opt/datos/plotting_25/thermophilus_codon.tsv' df = pd.read_csv(infile,sep='\t') # df es un DataFrame df.plot(kind='bar',x='Codon',y='Frequency',xlabel='Codon',ylabel='Frequency (per 1k)',title="Codon Frequency of Thermus thermophilus ") plt.show()
Para guardar el gráfico df.plot() ax = df.plot() plt.savefig("plot1.png") plt.close()
¿Como podemos graficar la frecuencia para cada aminoácido?
Solución: los métodos groupby() y agg()
grouped_df = df.groupby('AA',as_index=False).agg(Frequency=('Frequency',"sum"))
print(grouped_df.head())
grouped_df['sample']='thermo'
Usar plotly
import plotly.express as px
fig = px.bar(grouped_df,x='AA',y='Frequency') fig.show() exit()
Comparar datos
Tarea1:
Tenemos otra salida del cusp: /opt/datos/plotting_25/NC_007633_codon.tsv
Generar un gráfico comparando las frecuencias de los dos ficheros