two DataFrame plot in a single plot matplotlip

2024/11/16 0:31:48

I want to plot two DataFrame in a single plot.Though, I have seen similar post but none seems to work out.

First 5 rows of my dataframe looks like this:

df1

    name    type        start       stop    strand
0   geneA   transcript  2000        7764        +
1   geneA   exon        2700        5100        +
2   geneA   exon        6000        6800        +
3   geneB   transcript  9000        12720       -
4   geneB   exon        9900        10100       -

df2

    P1      P2       P3      P4
0   0.28    0.14    0.19    0.19
1   0.30    0.16    0.17    0.20
2   0.26    0.13    0.20    0.12
3   0.21    0.13    0.25    0.15
4   0.31    0.03    0.24    0.20

I want the plot to look like this:

sample plot

I tried doing this:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
ax = df1.plot()df1.plot(ax=ax)

but, the output was not meaningful. I will appreciate suggestions/solutions on how to achieve this.

Answer

Here is a minimal example:

import matplotlib.pyplot as pltf, axes = plt.subplots(nrows=len(df2.columns)+1, sharex=True, )# plots for df2 columns
i = 0
for col in df2.columns:df2[col].plot(ax=axes[i])axes[i].set_ylim(0, 1.2)axes[i].set_ylabel(col)i+=1## code to plot annotations
# axes[-1].plot(…)
axes[-1].set_xlabel('Genomic position')# remove space between plots
plt.subplots_adjust(hspace=0)

genomic plot

Here is the full graph:

f, axes = plt.subplots(nrows=len(df2.columns)+1, sharex=True, )# plots for df2 columns
i = 0
for col in df2.columns:df2[col].plot(ax=axes[i], color='#505050')axes[i].set_ylim(0, 1.3)axes[i].set_ylabel(col)i+=1## code to plot annotations
axes[-1].set_xlabel('Genomic position')
axes[-1].set_ylabel('annotations')
axes[-1].set_ylim(-0.5, 1.5)
axes[-1].set_yticks([0, 1])
axes[-1].set_yticklabels(['−', '+'])for _, r in df1.iterrows():marker = '|'lw=1if r['type'] == 'exon':marker=Nonelw=8y = 1 if r['strand'] == '+' else 0axes[-1].plot((r['start'], r['stop']), (y, y),marker=marker, lw=lw,solid_capstyle='butt',color='#505050')# remove space between plots
plt.subplots_adjust(hspace=0)

genomic data

https://en.xdnf.cn/q/119250.html

Related Q&A

Automate `lxc-attach` through ssh with Python

Question: How do I automate this process, and include all of the password prompts? Machine 1> ssh user2@machine2password: Machine 2> lxc-attach -n 0x1000 Container> ssh user3@machine3password…

Value of a key in a Python dictinary is not updating [duplicate]

This question already has answers here:Appending a dictionary to a list - I see a pointer like behavior(3 answers)Python The appended element in the list changes as its original variable changes(1 answ…

Python - __init__() missing 1 required positional argument:

Im kinda new to python and I cant get past this error: Traceback (most recent call last):File "***", line 63, in <module>bst = Node() TypeError: __init__() missing 1 required positional…

discord py - Custom command prefix doesnt work (no command run)

i have a problem that i cant solve. Im trying to add a prefix switcher for all guilds, that uses my bot. So Ive done that, but currently no command gets triggered and I cant find a solution since hours…

How to use sep parameter in .format?

I just started learning python and Im experimenting new things. isim = input("Name:") soyad = input("Surname:") yaş = input("Age:") edu = input("Education:") ge…

Python table classification

I have different type of data for example:4.5,3.5,U1 4.5,10.5,U2 4.5,6,U1 3.5,10.5,U2 3.5,10.5,U2 5,7,U1 7,6.5,U1I need output:U1: [[4.5, 3.5], [4.5, 6], [5, 7], [7, 6.5]] U2: [[4.5, 10.5], [3.5, 10.5]…

python json.loads / json.load truncates nested json objects?

given the following code: import json foo = {"root":"cfb-score","children":{"gamecode":{"attribute":"global-id"},"gamestate":{&quo…

How to make an encrypted executable file

I have made a tool/program on Ubuntu written in Python. I want to give this to my friend to test on his PC, but I dont want to share the source code.This program has many folders and many .py files. Is…

Organizing pythonic dictionaries for a JSON schema validation

Scenario: I am trying to create a JSON schema validator in python. In this case, I am building a dictionary which contain the information that will be used for the validation.Code:import json import os…

Scraping a specific website with a search box and javascripts in Python

On the website https://sray.arabesque.com/dashboard there is a search box "input" in html. I want to enter a company name in the search box, choose the first suggestion for that name in the d…