在一个图中叠加两个密度

时间:2015-10-19 14:44:27

标签: python pandas matplotlib

我有以下函数绘制a_positions的密度。现在,我想添加b_positions,其中a_positions与trasparent颜色重叠。如何使用以下函数在一个图中叠加a_positions和b_positions?

def plot_density(a_positions, b_positions, chr_name, chr_len, out_fname):
    d = pd.DataFrame(a_positions)
    d.hist(bins=100)
    ax = plt.gca()
    ax.set_xlabel(chr_name + " positions")
    ax.set_ylabel("Density")

    ax.set_title('', color='black')
    #plt.subplots_adjust(bottom=.25, left=.25)
    plt.ticklabel_format(style='plain')

    ax.set_xlim(xmin=1, xmax=chr_len)
    ax.set_xticks(pd.np.linspace(1, chr_len, 5))
    plt.savefig(out_fname)
    plt.close()

1 个答案:

答案 0 :(得分:1)

alpha接受alpha关键字,该关键字控制透明度。因此,如果您将b_positions的{​​{1}}设置为<1,那么您应该可以在其下方看到a_positions

这是一个简单的例子:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.random.rand(100))
df2 = pd.DataFrame(np.random.rand(100))

df1.hist(bins=10,color='b')
df2.hist(bins=10,ax=plt.gca(),alpha=0.5,color='r')

plt.show()

enter image description here