【问题标题】:two DataFrame plot in a single plot matplotlip一个图matplotlib中的两个DataFrame图
【发布时间】:2021-09-22 03:17:00
【问题描述】:

我想在一个图中绘制两个 DataFrame。不过,我看过类似的帖子,但似乎都没有。

我的数据框的前 5 行如下所示:

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

我希望情节看起来像这样:

我试过这样做:

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

df1.plot(ax=ax)

但是,输出没有意义。 我将感谢有关如何实现这一目标的建议/解决方案。

【问题讨论】:

  • 我不确定这个问题在当前状态下是否可以回答。目前尚不清楚您显示的数据如何成为显示的图。线条粗细的差异来自哪里?您如何处理重叠的变量范围?你如何统一x轴?等等等等。
  • @Henry,我是 python 及其绘图库的新手。据我了解,y 轴有 df2 和 df1 的 strand 列,x 轴有 df1,只有 start stop 列。
  • 但这并不是那么简单。绘图需要绘制 points。例如plt.plot([0, 1, 2, 3], [0, 1, 2, 3]) 将从[0, 3] 绘制线y=x。你没有情节点。您有需要以某种方式转换为可以绘制的 points 的间隔。但是您没有指定如何处理重叠范围,甚至没有指定如何将范围转换为点。线图也没有像输出中的波形那样的锯齿状方差。从数据到指定的地块需要完成很多工作。
  • 就像你说的,我认为还有很多事情要做! df2 链接到 +- 值。但是关于锯齿状的差异,我不明白
  • 您需要一个基本教程,这与 SO 无关。这不是一个免费的编码网站,“需要做很多事情”可能意味着你来错地方了。

标签: python pandas numpy matplotlib


【解决方案1】:

您可以使用subplots 这样做(因为很难理解应该如何绘制两个df,所以我提供了一个通用示例)

Import matplotlib.pyplot as plt

fig,axes = plt.subplots(4,1) #4 rows, one column
for ax in axes:
   plt.plot(X1,y1 ax =ax) # loop over each subplot and create a plot
   plt.plot(X2,y2, ax = ax)

【讨论】:

  • @CutPoison,我可以在哪里添加示例中的df1df2?我用它代替 x1y1 但我得到了错误
【解决方案2】:

这是一个最小的例子:

import matplotlib.pyplot as plt

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])
    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)

这是完整的图表:

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=1
    if r['type'] == 'exon':
        marker=None
        lw=8
    y = 1 if r['strand'] == '+' else 0
    axes[-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)

【讨论】:

  • @ mozway ,您的示例几乎完全符合我的要求。我只需要弄清楚如何在情节底部获得 3 box plots。谢谢!
  • 查看我对问题的评论。你能提供完整的数据集吗?
  • 链接到文件?
  • 我更新了图表,我会让你自己尝试找到如何自定义刻度标签和垂直线;)
  • @mozway,哇!你是最棒的! .你让我今天一整天都感觉很好!谢谢。这个命令 lw=lw 意味着什么?
猜你喜欢
  • 2019-01-15
  • 1970-01-01
  • 2020-03-03
  • 1970-01-01
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-20
相关资源
最近更新 更多