matplotlib和子图属性 [英] matplotlib and subplots properties
问题描述
我正在将matplotlib图形添加到画布,以便可以将其与pyqt集成到我的应用程序中.我环顾四周,似乎是使用plt.add_subplot(111)
的方式(?),但是我无法像使用普通"图一样向子图添加任何属性
I'm adding a matplotlib figure to a canvas so that I may integrate it with pyqt in my application. I were looking around and using plt.add_subplot(111)
seem to be the way to go(?) But I cannot add any properties to the subplot as I may with an "ordinary" plot
图设置
self.figure1 = plt.figure()
self.canvas1 = FigureCanvas(self.figure1)
self.graphtoolbar1 = NavigationToolbar(self.canvas1, frameGraph1)
hboxlayout = qt.QVBoxLayout()
hboxlayout.addWidget(self.graphtoolbar1)
hboxlayout.addWidget(self.canvas1)
frameGraph1.setLayout(hboxlayout)
创建子图并添加数据
df = self.quandl.getData(startDate, endDate, company)
ax = self.figure1.add_subplot(111)
ax.hold(False)
ax.plot(df['Close'], 'b-')
ax.legend(loc=0)
ax.grid(True)
我想设置x和y标签,但是如果我做ax.xlabel("Test")
I'd like to set x and y labels, but if I do ax.xlabel("Test")
AttributeError: 'AxesSubplot' object has no attribute 'ylabel'
如果我不使用子图就可以做到这一点
which is possible if I did it by not using subplot
plt.figure(figsize=(7, 4))
plt.plot(df['Close'], 'k-')
plt.grid(True)
plt.legend(loc=0)
plt.xlabel('value')
plt.ylabel('frequency')
plt.title('Histogram')
locs, labels = plt.xticks()
plt.setp(labels, rotation=25)
plt.show()
所以我想我的问题是,是否有可能进一步修改子图?还是我可以在不使用子图的情况下在pyqt画布中绘制图,这样我就可以从图的更多属性中受益.
So I guess my question is, is it not possible to modify subplots further? Or is it possible for me to plot graphs in a pyqt canvas, without using subplots so that I may get benefit of more properties for my plots.
推荐答案
plt.subplot
返回一个子图对象,该对象是轴对象的一种类型.它有两种添加轴标签的方法: set_xlabel
和 set_ylabel
:
plt.subplot
returns a subplot object which is a type of axes object. It has two methods for adding axis labels: set_xlabel
and set_ylabel
:
ax = plt.subplot('111')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
您还可以调用plt.xlabel
和plt.ylabel
(就像以前一样)并指定要应用标签的轴.
You could also call plt.xlabel
and plt.ylabel
(like you did before) and specify the axes to which you want the label applied.
ax = plt.subplot('111')
plt.xlabel('X Axis', axes=ax)
plt.ylabel('Y Axis', axes=ax)
由于只有一个轴,因此也可以省略axes
kwarg,因为如果未指定一个轴,标签将自动应用于当前轴.
Since you only have one axes, you could also omit the axes
kwarg since the label will automatically be applied to the current axes if one isn't specified.
ax = plt.subplot('111')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
这篇关于matplotlib和子图属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!