本文整理汇总了Python中matplotlib.backends.backend_qt4agg.FigureCanvasQTAgg.get_width_height方法的典型用法代码示例。如果您正苦于以下问题:Python FigureCanvasQTAgg.get_width_height方法的具体用法?Python FigureCanvasQTAgg.get_width_height怎么用?Python FigureCanvasQTAgg.get_width_height使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类matplotlib.backends.backend_qt4agg.FigureCanvasQTAgg
的用法示例。
在下文中一共展示了FigureCanvasQTAgg.get_width_height方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CourseMapDialog
# 需要导入模块: from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg [as 别名]
# 或者: from matplotlib.backends.backend_qt4agg.FigureCanvasQTAgg import get_width_height [as 别名]
#.........这里部分代码省略.........
max_time = 0
for log_data in self._log_data.itervalues():
bounds = log_data.bounds
minx = min(minx, bounds[0][0])
miny = min(miny, bounds[0][1])
maxx = max(maxx, bounds[1][0])
maxy = max(maxy, bounds[1][1])
max_time = max(max_time, log_data.utm_data[-1][0])
self._total_time = max_time
x_size = maxx - minx
y_size = maxy - miny
xy_delta = x_size - y_size
if xy_delta > 0:
miny -= xy_delta / 2
maxy += xy_delta / 2
else:
minx += xy_delta / 2
maxx -= xy_delta / 2
self._bounds = ((minx, miny), (maxx, maxy))
self._plot.set_xlim(left=minx, right=maxx)
self._plot.set_ylim(bottom=miny, top=maxy)
self._canvas.draw()
def _handle_mouse(self, event):
if not event.inaxes or event.button != 1:
return
if self._mouse_start is None:
self._mouse_start = event
return
# Handle a pan event. What we want is for the point (data)
# where the mouse was originally clicked to stay under the
# pointer.
(width, height) = self._canvas.get_width_height()
px_x = (self._bounds[1][0] - self._bounds[0][0]) / width
px_y = (self._bounds[1][1] - self._bounds[0][1]) / height
x_change = (self._mouse_start.x - event.x) * px_x
y_change = (self._mouse_start.y - event.y) * px_y
self._plot.set_xlim(left=self._bounds[0][0] + x_change,
right=self._bounds[1][0] + x_change)
self._plot.set_ylim(bottom=self._bounds[0][1] + y_change,
top=self._bounds[1][1] + y_change)
self._canvas.draw()
def _update_bounds(self):
xlim = self._plot.get_xlim()
ylim = self._plot.get_ylim()
self._bounds = ((xlim[0], ylim[0]), (xlim[1], ylim[1]))
def _handle_mouse_release(self, event):
if event.button != 1:
return
self._mouse_start = None
self._update_bounds()
def _handle_scroll(self, event):
# Determine the relative offset of the clicked position to the
# center of the frame so that we can keep the data under the
# cursor.
(width, height) = self._canvas.get_width_height()
x_off = float(width - event.x) / width
y_off = float(height - event.y) / height
x_scale = (self._bounds[1][0] - self._bounds[0][0]) * (event.step / 10.)
y_scale = (self._bounds[1][1] - self._bounds[0][1]) * (event.step / 10.)
# Check if we've tried to zoom to far and would invert our axes
new_xlim = (self._bounds[0][0] + (x_scale * (1. - x_off)),
self._bounds[1][0] - (x_scale * x_off))
new_ylim = (self._bounds[0][1] + (y_scale * (1. - y_off)),
self._bounds[1][1] - (y_scale * y_off))
if (new_xlim[1] <= new_xlim[0]) or (new_ylim[1] <= new_ylim[0]):
return
self._plot.set_xlim(left=new_xlim[0], right=new_xlim[1])
self._plot.set_ylim(bottom=new_ylim[0], top=new_ylim[1])
self._update_bounds()
self._canvas.draw()
def update_time(self, new_time, update_slider=True):
# Bound the time to our useful range.
new_time = max(0, min(new_time, self._total_time))
self._time_current = new_time
for log_data in self._log_data.itervalues():
log_data.update_marker(new_time)
self._canvas.draw()
self._ui.elapsedTime.setText(str(new_time))
# if update_slider:
# self._ui.timeSlider.setValue(1000 * (new_time / self._total_time))
def _handle_time_slider(self):
if self._total_time == 0:
return
current = self._ui.timeSlider.value() / 1000.
self.update_time(current * self._total_time, update_slider=False)
self.time_slider_changed.emit(self._time_current)
示例2: __init__
# 需要导入模块: from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg [as 别名]
# 或者: from matplotlib.backends.backend_qt4agg.FigureCanvasQTAgg import get_width_height [as 别名]
#.........这里部分代码省略.........
self.figure.add_axes(self.axes)
self.axes.set_aspect(1)
self.axes.grid(True)
# Re-draw
self.canvas.draw_idle()
def adjust_axes(self, xmin, ymin, xmax, ymax):
"""
Adjusts all axes while maintaining the use of the whole canvas
and an aspect ratio to 1:1 between x and y axes. The parameters are an original
request that will be modified to fit these restrictions.
:param xmin: Requested minimum value for the X axis.
:type xmin: float
:param ymin: Requested minimum value for the Y axis.
:type ymin: float
:param xmax: Requested maximum value for the X axis.
:type xmax: float
:param ymax: Requested maximum value for the Y axis.
:type ymax: float
:return: None
"""
# FlatCAMApp.App.log.debug("PC.adjust_axes()")
width = xmax - xmin
height = ymax - ymin
try:
r = width / height
except ZeroDivisionError:
FlatCAMApp.App.log.error("Height is %f" % height)
return
canvas_w, canvas_h = self.canvas.get_width_height()
canvas_r = float(canvas_w) / canvas_h
x_ratio = float(self.x_margin) / canvas_w
y_ratio = float(self.y_margin) / canvas_h
if r > canvas_r:
ycenter = (ymin + ymax) / 2.0
newheight = height * r / canvas_r
ymin = ycenter - newheight / 2.0
ymax = ycenter + newheight / 2.0
else:
xcenter = (xmax + xmin) / 2.0
newwidth = width * canvas_r / r
xmin = xcenter - newwidth / 2.0
xmax = xcenter + newwidth / 2.0
# Adjust axes
for ax in self.figure.get_axes():
if ax._label != 'base':
ax.set_frame_on(False) # No frame
ax.set_xticks([]) # No tick
ax.set_yticks([]) # No ticks
ax.patch.set_visible(False) # No background
ax.set_aspect(1)
ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))
ax.set_position([x_ratio, y_ratio, 1 - 2 * x_ratio, 1 - 2 * y_ratio])
# Sync re-draw to proper paint on form resize
self.canvas.draw()
def auto_adjust_axes(self, *args):
"""
示例3: DrawingPad_Painter
# 需要导入模块: from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg [as 别名]
# 或者: from matplotlib.backends.backend_qt4agg.FigureCanvasQTAgg import get_width_height [as 别名]
#.........这里部分代码省略.........
self.hbox_layout.addWidget(self.canvas, 5)
self.line_width = 12.0
# self.ctrl_pnl_layout = QVBoxLayout()
# #a button to clear the figure
# self.clean_btn = QPushButton('Clear')
# self.ctrl_pnl_layout.addWidget(self.clean_btn)
#
# self.hbox_layout.addLayout(self.ctrl_pnl_layout, 1)
self.setLayout(self.hbox_layout)
self.drawing = False
self.create_event_handler()
return
def create_event_handler(self):
self.canvas_button_clicked_cid = self.canvas.mpl_connect('button_press_event', self.on_canvas_mouse_clicked)
self.canvas_button_released_cid = self.canvas.mpl_connect('button_release_event', self.on_canvas_mouse_released)
self.canvas_motion_notify_cid = self.canvas.mpl_connect('motion_notify_event', self.on_canvas_mouse_move)
return
def on_canvas_mouse_clicked(self, event):
# print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
# event.button, event.x, event.y, event.xdata, event.ydata)
self.drawing = True
# create a new line if we are drawing within the area
if event.xdata is not None and event.ydata is not None and self.curr_line is None and self.curr_traj is None:
self.curr_line, = self.ax_painter.plot([event.xdata], [event.ydata], '-k', linewidth=self.line_width)
self.curr_traj = [np.array([event.xdata, event.ydata])]
self.canvas.draw()
return
def on_canvas_mouse_released(self, event):
# print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
# event.button, event.x, event.y, event.xdata, event.ydata)
self.drawing = False
# store finished line and trajectory
# print self.curr_traj
self.lines.append(self.curr_line)
self.traj_pnts.append(self.curr_traj)
self.curr_traj = None
self.curr_line = None
return
def on_clean(self, event):
print 'clean the canvas...'
#clear everything
for line in self.ax_painter.lines:
self.ax_painter.lines.remove(line)
self.lines = []
if self.curr_line is not None:
self.ax_painter.lines.remove(self.curr_line)
self.curr_line = None
self.canvas.draw()
self.traj_pnts = []
self.curr_traj = None
self.drawing = False
return
def on_canvas_mouse_move(self, event):
if self.drawing:
# print 'In movement: x=',event.x ,', y=', event.y,', xdata=',event.xdata,', ydata=', event.ydata
if event.xdata is not None and event.ydata is not None and self.curr_line is not None and self.curr_traj is not None:
#append new data and update drawing
self.curr_traj.append(np.array([event.xdata, event.ydata]))
tmp_curr_data = np.array(self.curr_traj)
self.curr_line.set_xdata(tmp_curr_data[:, 0])
self.curr_line.set_ydata(tmp_curr_data[:, 1])
self.canvas.draw()
return
def plot_trajs_helper(self, trajs):
tmp_lines = []
for traj in trajs:
tmp_line, = self.ax_painter.plot(traj[:, 0], traj[:, 1], '-.g', linewidth=self.line_width)
tmp_lines.append(tmp_line)
self.canvas.draw()
#add these tmp_lines to lines record
self.lines = self.lines + tmp_lines
return
def get_traj_data(self):
return self.traj_pnts
def get_image_data(self):
"""
Get the deposited image
"""
w,h = self.canvas.get_width_height()
buf = np.fromstring ( self.canvas.tostring_argb(), dtype=np.uint8 )
buf.shape = ( w, h, 4 )
# canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode
buf = np.roll ( buf, 3, axis = 2 )
return buf
示例4: ImageViewer
# 需要导入模块: from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg [as 别名]
# 或者: from matplotlib.backends.backend_qt4agg.FigureCanvasQTAgg import get_width_height [as 别名]
import sys
app = QtGui.QApplication(sys.argv)
imageViewer = ImageViewer()
imageViewer.show()
figure = plt.figure()
figure.set_size_inches(11, 8.5)
figure.patch.set_facecolor('white')
plt.plot([1, 2, 3, 4], [1, 2, 3, 4], '-b')
figure_canvas = FigureCanvasQTAgg(figure)
figure_canvas.draw()
size = figure_canvas.size()
width, height = size.width(), size.height()
print(width, height)
print(figure_canvas.get_width_height())
imgbuffer = figure_canvas.buffer_rgba()
image = QtGui.QImage(imgbuffer, width, height,
QtGui.QImage.Format_ARGB32)
# Reference for the RGB to BGR swap:
# http://sourceforge.net/p/matplotlib/mailman/message/5194542/
image = QtGui.QImage.rgbSwapped(image)
imageViewer.load_image(image, 0)
sys.exit(app.exec_())