编写函数:带有plt的绘图条,带有plt.title()

时间:2017-03-09 11:12:08

标签: python function matplotlib python-3.5

def plot_coexpression(new, gene1='', gene2='', gene3='', gene4='', gene5='', gene6=''):
    X, Y = zip(*new)
    import seaborn as sns
    sns.set()
    import matplotlib.pyplot as plt 
    %matplotlib inline
    plt.figure(figsize = (20, 10))
    plt.title('Genes most commonly co-expressed with' gene1="axin", gene2="lef", gene3="lgr", gene4="nkd", gene5="", gene6="", fontsize=40)

    ax = plt.bar(range(len(X)), Y, 0.6, align='center', tick_label = X, color="green") 
    ax = plt.xticks(rotation=90)


  File "<ipython-input-3-a3383d66ce8c>", line 8
    plt.title('Genes most commonly co-expressed with' gene1="axin", gene2="lef", gene3="lgr", gene4="nkd", gene5="", gene6="", fontsize=40)

我想用plt.title绘制plt条(&#39; Genes最常用的共同表达基因1 =&#34; axin&#34;,gene2 =&#34; lef&#34; ,gene3 =&#34; lgr&#34;,gene4 =&#34; nkd&#34;,gene5 =&#34;&#34;,gene6 =&#34;&#34;,)

gene1到gene6名称是我函数中的可选参数。如果没有定义,它们不应出现在情节标题中。

此替代代码也不起作用。

def plot_coexpression(new, gene1=None, gene2=None, gene3=None, gene4=None, gene5=None, gene6=None):
    X, Y = zip(*new)
    plt.figure(figsize = (20, 10))
    plt.title('Genes most commonly co-expressed with' gene1, gene2, gene3, gene4, gene5, gene6,"'", fontsize=40)
    ax = plt.bar(range(len(X)), Y, 0.6, align='center', tick_label = X, color="green") 
    ax = plt.xticks(rotation=90)

1 个答案:

答案 0 :(得分:0)

你不能简单地将任意参数放到一个不期望它们的函数中。

plt.title期望一个字符串作为输入。

生成字符串可以使用format方法

完成
mytitle = "This is my title, containing {gene1}".format(gene1="axin")
plt.title(mytitle)