【问题标题】:How to put a variable in x/y label using matplotlib module pyplot如何使用 matplotlib 模块 pyplot 将变量放入 x/y 标签中
【发布时间】:2012-05-13 11:00:47
【问题描述】:

我在 Python3.2 中生成堆叠条形图并使用“Matplotlib”模块“pyplot”。唯一的问题是,在“xlabel”中,我必须包含一个变量(整数)来表示分析的条目数。我已经尝试了很多,但找不到将变量包含在 xlabel 中的方法。

我的部分代码:

plt.ylabel('Percentage', fontproperties=font_manager.FontProperties(size=10))
plt.xlabel('position', fontproperties=font_manager.FontProperties(size=10))
plt.title("'Graph-A",fontproperties=font_manager.FontProperties(size=10))

plt.xticks(np.arange(22), np.arange(1,23), fontproperties=font_manager.FontProperties(size=8))
plt.yticks(np.arange(0,121,5), fontproperties=font_manager.FontProperties(size=8))
plt.legend((p1[0], p2[0], p3[0], p4[0],p5[0]), ('A','B','C','D','E'), loc=1, prop=font_manager.FontProperties(size=7))

变量“条目”保存已处理条目的值,我需要将此值包含在 xlabel 中。请帮忙?

AK

【问题讨论】:

  • 为了清楚起见,您想将变量传递给plt.xlabel?

标签: python label matplotlib python-3.2


【解决方案1】:

如果我没听错,你可以试试:

plt.xlabel('position %d' % entries, fontproperties=font_manager.FontProperties(size=10))

【讨论】:

  • 谢谢。我不认为它会起作用,这就是为什么没有测试。
【解决方案2】:

你可以试试这个。它对我有用。

plt.xlabel('position'+str(entries), fontproperties=font_manager.FontProperties(size=10))

我已经在我的代码中使用了它

plt.plot(qtime,hy_p,'r.')
plt.xlabel('Time')
plt.ylabel('Hz at node '+str(50))

【讨论】:

    【解决方案3】:

    这就是你在 python 3.6 中可以做的事情。

    plt.xlabel('Your variable is put here: {}'.format(variable))

    【讨论】:

      猜你喜欢
      • 2013-04-21
      • 2019-09-30
      • 2018-07-14
      • 2018-08-01
      • 2019-09-20
      • 2020-09-14
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      相关资源
      最近更新 更多