【问题标题】:two DataFrame plots两个 DataFrame 图
【发布时间】:2021-07-16 02:15:52
【问题描述】:

我的情节与下面链接中回答的情节相似:

two DataFrame plot in a single plot matplotlip

我对@9​​87654324@ 做了一些修改,因为我认为这是我必须修改的地方,但我无法产生输出。 我想要的情节样本是

这就是我修改它的方式:

f, axes = plt.subplots(nrows=len(signals.columns)+1, sharex=True, )
i = 0
for col in df2.columns:
    fig, axs = plt.subplots()
    sns.regplot(x='', y='', data=df2, ax=axs[0])
        df2[col].plot(ax=axes[i], color='grey')
        axes[i].set_ylabel(col)
        i+=1
I have seen that its wrong. 


我试过了,感觉不错:)

如何对此进行修改以获得我想要的:

f, axes = plt.subplots(nrows=len(signals.columns)+1, sharex=True, )

# plots for df2 columns
i = 0
for col in df2.columns:
    lw=1
    df2[col].plot(ax=axes[i], color='grey')
    axes[i].set_ylim(0, 1)
    axes[i].set_ylabel(col)
sns.rugplot(df2["P1"])

【问题讨论】:

  • 顺便说一句,您是如何生成此图的?这是做功课吗?如果是,您应该自己尝试一下。
  • 图表来自学习平台。不是家庭作业。
  • 但重点是你要学习,对吧?您能否发布您尝试生成的代码以及最终的错误?
  • 这是我尝试过的:f, axes = plt.subplots(nrows=len(signals.columns)+1, sharex=True, ) i = 0 for col in df2.columns: fig, axs = plt.subplots() sns.regplot(x='', y='', data=df2, ax=axs[0]) df2[col].plot(ax=axes[i], color='grey') axes[i].set_ylabel(col) i+=1 我已经看到这是绝对错误的!
  • 请编辑您的问题以发布您的代码

标签: python pandas numpy matplotlib


【解决方案1】:

您有多种选择来制作此图表。 df1 和 df2 定义在your previous question

matplotlib.pyplot.scatter 的版本绘制速度更快,但对示例的忠实度较低。带有seaborn.rugplot 的版本看起来与示例相同,但绘制时间更长。我在注释行之间突出显示了代码的重要部分########

使用matplotlib.pyplot.scatter

import seaborn as sns
import numpy as np

f, axes = plt.subplots(nrows=len(df2.columns)+1, sharex=True,
                       gridspec_kw={'height_ratios':np.append(np.repeat(1, len(df2.columns)), 3)})

####### variable part below #######

# plots for df2 columns
i = 0
for col in df2.columns:
    axes[i].scatter(x=df2.index, y=np.repeat(0, len(df2)), c=df2[col], marker='|', cmap='Greys')
    axes[i].set_ylim(-0.5, 0.5)
    axes[i].set_yticks([0])
    axes[i].set_yticklabels([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)

axes[-1].set_xlim(0, len(df2))

f.set_size_inches(6, 2)

使用seaborn.rugplot

import seaborn as sns
import numpy as np

f, axes = plt.subplots(nrows=len(df2.columns)+1, sharex=True,
                       gridspec_kw={'height_ratios':np.append(np.repeat(1, len(df2.columns)), 3)})

####### variable part below #######

import matplotlib
import matplotlib.cm as cm
norm = matplotlib.colors.Normalize(vmin=0, vmax=1, clip=True)
mapper = cm.ScalarMappable(norm=norm, cmap=cm.Greys)

# plots for df2 columns
i = 0
for col in df2.columns:
    sns.rugplot(x=df2.index, color=list(map(mapper.to_rgba, df2[col])), height=1, ax=axes[i])
    axes[i].set_yticks([0])
    axes[i].set_yticklabels([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)

axes[-1].set_xlim(0, len(df2))

f.set_size_inches(6, 2)

【讨论】:

    猜你喜欢
    • 2021-09-22
    • 2016-10-24
    • 2020-08-18
    • 2015-06-14
    • 2020-05-03
    • 2013-03-03
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    相关资源
    最近更新 更多