【问题标题】:Writing Function: Plot Bar with plt with optional arguments for plt.title()编写函数:带有 plt 的绘图栏,带有 plt.title() 的可选参数
【发布时间】:2017-07-30 08:23:53
【问题描述】:
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('Genes most common co-expressed with'gene1="axin",gene2="lef",gene3="lgr",gene4="nkd",gene5= 绘制 plt 条"",gene6="",)

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)

【问题讨论】:

    标签: python function matplotlib python-3.5


    【解决方案1】:

    您不能简单地将任意参数放入不期望它们的函数中。

    plt.title 需要一个字符串作为输入。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多