0

我正在绘制一个折线图和一个条形图,matplotlib并且两者都单独使用我的脚本。
但我面临一个问题:
1. 如果我想在同一个输出窗口中绘制两个图
2. 如果我想将显示窗口自定义为 1024*700

在第一种情况下,我使用 subplot 在同一个窗口中绘制两个图,但我无法为这两个图提供它们各自的 x 轴和 y 轴名称以及它们各自的标题。我失败的代码是:

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
xs,ys = np.loadtxt("c:/users/name/desktop/new folder/x/counter.cnt",delimiter = ',').T
fig = plt.figure()
lineGraph = fig.add_subplot(211)
barChart = fig.add_subplot(212)

plt.title('DISTRIBUTION of NUMBER')
lineGraph = lineGraph.plot(xs,ys,'-')  #generate line graph
barChart = barChart.bar(xs,ys,width=1.0,facecolor='g') #generate bar plot

plt.grid(True)
plt.axis([0,350,0,25])  #controlls axis for charts x first and then y axis.


plt.savefig('new.png',dpi=400)
plt.show()

但有了这个,我无法正确标记这两个图。
还请提供一些关于如何将窗口大小调整为 1024*700 的想法。

4

2 回答 2

1

当你说

我正在使用 subplot 在同一个窗口中绘制两个图表,但我无法为这两个图表提供它们各自的 x 轴和 y 轴名称以及它们各自的标题。

你的意思是你想设置轴标签?如果是这样,请尝试使用lineGraph.set_xlabeland lineGraph.set_ylabel。或者,在创建绘图之后和创建任何其他绘图之前调用plt.xlabeland 。plot.ylabel例如

# Line graph subplot
lineGraph = lineGraph.plot(xs,ys,'-')
lineGraph.set_xlabel('x')
lineGraph.set_ylabel('y')

# Bar graph subplot
barChart = barChart.bar(xs,ys,width=1.0,facecolor='g')
barChart.set_xlabel('x')
barChart.set_ylabel('y')

这同样适用于标题。调用plt.title将为当前活动的情节添加标题。这是您创建的最后一个绘图或您使用的最后一个绘图plt.gca。如果您想要特定子图上的标题,请使用子图句柄:lineGraph.set_titlebarChart.set_title.

于 2012-05-17T09:51:20.383 回答
0

fig.add_subplot返回一个 matplotlib Axes 对象。如 Chris 所述,该对象上的方法包括set_xlabeland 。您可以在http://matplotlib.sourceforge.net/api/axes_api.htmlset_ylabel查看 Axes 对象上可用的全套方法。

于 2012-05-20T00:12:50.497 回答