Page History
...
Code Block |
---|
fig = plt.figure(num=1, figsize=(10,10), dpi=100, facecolor='w',edgecolor='w',frameon=True)
axes = fig.add_subplot(111)
axim = axes.imshow(arrwin, interpolation='nearest', origin='bottom', aspect='auto', extent=[xmin, xmax, ymax, ymin])
|
How to
...
clear figure
Code Block |
---|
axesfig.hist(arr, bins=100, range=(10,50), log=False) # log for vertical scale onlyclear() |
How to draw graphic
Code Block |
---|
axes.plot(xarr1, yarr1, '-r', xarr2, yarr2, '-g') |
How to
...
draw histogram
Code Block |
---|
axes.hist(arr, bins=100, range= fig.add_subplot(111) |
...
(10,50), log=False) # log for vertical scale only
|
How to draw image (with colorbar)
Code Block |
---|
axes1 img = figaxes.add_subplot(211) axes2 = fig.add_subplot(212)imshow(arr2d, interpolation='nearest', origin='bottom', aspect='auto') img.set_clim(Amin,Amax) cbar = self.fig.colorbar(img, orientation='vertical', \ |
How to make figure with non-equal subplots
Code Block |
---|
axgr = fig.add_axes([ fraction=0.1, pad=0.6401, shrink=1.0.80, 0.35]aspect=20) axhi = fig.add_axes([0.1, 0.14, 0.35, 0.35]) axti = fig.add_axes([0.55, 0.14, 0.35, 0.35]) |
or using grid:
Code Block |
---|
import matplotlib.gridspec as gridspec gs = gridspec.GridSpec(20, 20) # Naive direction # fraction - of the 2d plot occupied by the color bar # pad [- is Ya space between ,2d image and Xcolor ]bar axsa = fig.add_subplot(gs[ 1:16, 0:14]) axsb = fig.add_subplot(gs[ 1:16, 14:19]) axsc = fig.add_subplot(gs[16: , 0:14]) |
How to set axes limits
Code Block |
---|
axes.set_xlim((-50,50))
axes.set_ylim((-10,210))
|
How to draw line
Code Block |
---|
import matplotlib.lines as lines
line = lines.Line2D(arrx, arry, linewidth=1, color='r')
axes.add_artist(line)
|
How to draw axis without labels
Code Block |
---|
import matplotlib.ticker as mtick
axes.xaxis.set_major_formatter( mtick.NullFormatter() )
|
How to rotate axis labels
Code Block |
---|
for label in axes.get_xticklabels() :
label.set_rotation(60) # rotate by 60 degree
label.set_horizontalalignment('center') # 'right', etc.
|
How to set a number of ticks along the axis
Code Block |
---|
from matplotlib.ticker import MaxNLocator
axes.xaxis.set_major_locator(MaxNLocator(4))
|
How to change axis label position on the plot
Code Block |
---|
axes.xaxis.set_ticks_position('top')
axes.yaxis.set_ticks_position('right')
|
How to draw a color bar as a separate sub-plot
Code Block |
---|
axCB = fig.add_subplot(gs[ 0, 0:14])
colb = fig.colorbar(axim, cax=axCB, orientation='horizontal')
axCB.xaxis.set_ticks_position('top') # change position of axis labels
|
How draw text
Code Block |
---|
plt.text(x, y, text, fontsize=7, color='k', ha='left', rotation=45)
|
At the edge of matplotlib and PyQt4
We assume that everything is done in our backend basis:
Code Block |
---|
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
|
How to get the current matplotlib figure window position on monitor
Code Block |
---|
pos = fig.canvas.manager.window.pos()
print 'x,y=', pos.x(), pos.y()
|
How to move the matplotlib figure window in certain (x,y) position
Code Block |
---|
fig.canvas.manager.window.move(x,y) # in pixels from top-left corner
|
How to move the matplotlib figure window on top (of all pileup windows)
...
# shrink - factor for the length of the color bar
# aspect - ratio length/width of the color bar
|
How to draw a color bar as a separate sub-plot
Code Block |
---|
axcb = fig.add_axes([0.1, 0.1, 0.8, 0.1])
cbar = fig.colorbar(img, cax=axcb, orientation='horizontal', \
fraction=0.1, pad=0.01, shrink=1.0, aspect=20)
axcb.xaxis.set_ticks_position('top') # change position of axis labels
|
How to define subplot(s) with empty axes
Code Block |
---|
axes = fig.add_subplot(111)
|
or
Code Block |
---|
axes1 = fig.add_subplot(211)
axes2 = fig.add_subplot(212)
|
How to make figure with non-equal subplots
Code Block |
---|
axgr = fig.add_axes([0.1, 0.64, 0.80, 0.35])
axhi = fig.add_axes([0.1, 0.14, 0.35, 0.35])
axti = fig.add_axes([0.55, 0.14, 0.35, 0.35])
|
or using grid:
Code Block |
---|
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(20, 20)
# Naive direction [ Y , X ]
axsa = fig.add_subplot(gs[ 1:16, 0:14])
axsb = fig.add_subplot(gs[ 1:16, 14:19])
axsc = fig.add_subplot(gs[16: , 0:14])
|
How to set axes limits
Code Block |
---|
axes.set_xlim((-50,50))
axes.set_ylim((-10,210))
|
How to draw line
Code Block |
---|
import matplotlib.lines as lines
line = lines.Line2D(arrx, arry, linewidth=1, color='r')
axes.add_artist(line)
|
How to set a number of ticks along the axis
Code Block |
---|
from matplotlib.ticker import MaxNLocator
axes.xaxis.set_major_locator(MaxNLocator(4))
|
How to draw axis without tick-labels
Code Block |
---|
import matplotlib.ticker as mtick
axes.xaxis.set_major_formatter( mtick.NullFormatter() )
|
How to rotate axis tick-labels
Code Block |
---|
for label in axes.get_xticklabels() :
label.set_rotation(60) # rotate by 60 degree
label.set_horizontalalignment('center') # 'right', etc.
|
How to change axis tick-label position on the plot
Code Block |
---|
axes.xaxis.set_ticks_position('top')
axes.yaxis.set_ticks_position('right')
|
How to draw axes labels
Code Block |
---|
axes.set_xlabel('Time index')
axes.set_ylabel('dt(sec)')
|
How to draw text
Code Block |
---|
plt.text(x, y, text, fontsize=7, color='k', ha='left', rotation=45)
axes.text(x, y, text, fontsize=7, color='k', ha='left', rotation=45)
|
How to draw axes title
Code Block |
---|
plt.title('Image',color='b',fontsize=20)
or
axes.set_title('Image',color='b',fontsize=20)
|
How to draw window title
Code Block |
---|
fig.canvas.set_window_title('Image And Spectrum ' + u'\u03C6')
|
Add symbols in string
Code Block |
---|
str = 'Symbol phi: ' + u'\u03C6'
|
How to get canvas and connect it with mouse buttons
Code Block |
---|
canvas = fig.canvas
canvas.mpl_connect('button_press_event', self.processMouseButtonPress)
canvas.mpl_connect('button_release_event', self.processMouseButtonRelease)
canvas.mpl_connect('motion_notify_event', self.processMouseMotion)
...
def processMouseButtonPress(self, event) :
print 'MouseButtonPress'
print 'event: xdata, ydata, x, y =', event.xdata, event.ydata, event.x, event.y
if event.inaxes == self.axgr : self.mousePressOnGraph(event)
if event.inaxes == self.axti : self.mousePressOnGraph(event)
if event.inaxes == self.axhi : self.mousePressOnHisto(event)
if event.button == 1 : # 1=left, 2=middle, 3=right
self.gr_xmin = float(event.xdata)
|
At the edge of matplotlib and PyQt4
We assume that everything is done in our backend basis:
Code Block |
---|
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
|
How to get the current matplotlib figure window position on monitor
Code Block |
---|
pos = fig.canvas.manager.window.pos()
print 'x,y=', pos.x(), pos.y()
h3. How to move the matplotlib figure window in certain (x,y) position
|
fig.canvas.manager.window.move(x,y) # in pixels from top-left corner
Code Block |
---|
or
|
plt.get_current_fig_manager().window.move(90, 100)
Code Block |
---|
For our backend starting from python 2.7.2:
|
plt.get_current_fig_manager().window.geometry("+100+300")
Code Block |
---|
h3. How to move the matplotlib figure window on top (of all pileup windows)
|
fig.canvas.manager.window.activateWindow()
...
#
...
Makes
...
window
...
active
...
fig.canvas.manager.window.raise_()
...
#
...
Moves
...
window
...
on
...
top
Code Block |
---|
the attribute trick is that the {{fig.canvas.manager.window}} returns the {{QtGui.QMainWindow}}, which is subclass of {{QtGui.QWidget}} with all that useful methods |
...
.
h3. How to add figure as a widget of the QtGui
|
fig =
How to add figure as a widget of the QtGui
...
plt.figure(num=None
...
figsize=(5,10),
...
dpi=100,
...
facecolor='w',edgecolor='w',frameon=True)
...
vbox
...
=
...
QtGui.QVBoxLayout()
...
vbox.addWidget(fig.canvas)
...
#
...
Wraps
...
figure
...
canvas
...
in
...
widget
...
self.setLayout(vbox)
...
Code Block |
---|
h3. How to receive a signal in program when window is activated by the mouse click on frame ?
I do not know yet...
But, if you click on figure canvas (the region inside the window frame, use
|
How to receive a signal in program when window is activated by the mouse click on frame ?
I do not know yet...
But, if you click on figure canvas (the region inside the window frame, use
...
fig.canvas.mpl_connect('button_press_event',
...
self.onButtonPressEvent)
...
#Should
...
be
...
implemented
...
something
...
like:
...
def
...
onButtonPressEvent(
...
self,
...
event
...
):
...
"""Figure
...
is
...
picked"""
print 'click on fig number =',
...
event.canvas.figure.number
...
Code Block |
---|
h3. How to receive a signal in program when close window by the click on "X"
|
How to receive a signal in program when close window by the click on "X"
...
fig.canvas.mpl_connect('close_event',
...
self.onCloseEvent)
...
#Should
...
be
...
implemented
...
something
...
like:
...
def
...
onCloseEvent(
...
self,
...
event
...
):
...
print 'close
...
event,
...
fig
...
number
...
=',
...
event.canvas.figure.number
...
How to close figure from program call
...
Code Block |
---|
h3. How to close figure from program call
|
plt.close(
...
num
...
)
...
#
...
close
...
figure
...
with
...
known
...
number
...
num
...
...
...
plt.close('all')
...
#
...
close
...
all
...
figures
Code Block |
---|