SourceForge logo
SourceForge logo
Menu

matplotlib-users — Discussion related to using matplotlib

You can subscribe to this list here.

2003 Jan
Feb
Mar
Apr
May
(3)
Jun
Jul
Aug
(12)
Sep
(12)
Oct
(56)
Nov
(65)
Dec
(37)
2004 Jan
(59)
Feb
(78)
Mar
(153)
Apr
(205)
May
(184)
Jun
(123)
Jul
(171)
Aug
(156)
Sep
(190)
Oct
(120)
Nov
(154)
Dec
(223)
2005 Jan
(184)
Feb
(267)
Mar
(214)
Apr
(286)
May
(320)
Jun
(299)
Jul
(348)
Aug
(283)
Sep
(355)
Oct
(293)
Nov
(232)
Dec
(203)
2006 Jan
(352)
Feb
(358)
Mar
(403)
Apr
(313)
May
(165)
Jun
(281)
Jul
(316)
Aug
(228)
Sep
(279)
Oct
(243)
Nov
(315)
Dec
(345)
2007 Jan
(260)
Feb
(323)
Mar
(340)
Apr
(319)
May
(290)
Jun
(296)
Jul
(221)
Aug
(292)
Sep
(242)
Oct
(248)
Nov
(242)
Dec
(332)
2008 Jan
(312)
Feb
(359)
Mar
(454)
Apr
(287)
May
(340)
Jun
(450)
Jul
(403)
Aug
(324)
Sep
(349)
Oct
(385)
Nov
(363)
Dec
(437)
2009 Jan
(500)
Feb
(301)
Mar
(409)
Apr
(486)
May
(545)
Jun
(391)
Jul
(518)
Aug
(497)
Sep
(492)
Oct
(429)
Nov
(357)
Dec
(310)
2010 Jan
(371)
Feb
(657)
Mar
(519)
Apr
(432)
May
(312)
Jun
(416)
Jul
(477)
Aug
(386)
Sep
(419)
Oct
(435)
Nov
(320)
Dec
(202)
2011 Jan
(321)
Feb
(413)
Mar
(299)
Apr
(215)
May
(284)
Jun
(203)
Jul
(207)
Aug
(314)
Sep
(321)
Oct
(259)
Nov
(347)
Dec
(209)
2012 Jan
(322)
Feb
(414)
Mar
(377)
Apr
(179)
May
(173)
Jun
(234)
Jul
(295)
Aug
(239)
Sep
(276)
Oct
(355)
Nov
(144)
Dec
(108)
2013 Jan
(170)
Feb
(89)
Mar
(204)
Apr
(133)
May
(142)
Jun
(89)
Jul
(160)
Aug
(180)
Sep
(69)
Oct
(136)
Nov
(83)
Dec
(32)
2014 Jan
(71)
Feb
(90)
Mar
(161)
Apr
(117)
May
(78)
Jun
(94)
Jul
(60)
Aug
(83)
Sep
(102)
Oct
(132)
Nov
(154)
Dec
(96)
2015 Jan
(45)
Feb
(138)
Mar
(176)
Apr
(132)
May
(119)
Jun
(124)
Jul
(77)
Aug
(31)
Sep
(34)
Oct
(22)
Nov
(23)
Dec
(9)
2016 Jan
(26)
Feb
(17)
Mar
(10)
Apr
(8)
May
(4)
Jun
(8)
Jul
(6)
Aug
(5)
Sep
(9)
Oct
(4)
Nov
Dec
2017 Jan
(5)
Feb
(7)
Mar
(1)
Apr
(5)
May
Jun
(3)
Jul
(6)
Aug
(1)
Sep
Oct
(2)
Nov
(1)
Dec
2018 Jan
Feb
Mar
Apr
(1)
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
2020 Jan
Feb
Mar
Apr
May
(1)
Jun
Jul
Aug
Sep
Oct
Nov
Dec
2025 Jan
(1)
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
S M T W T F S

1
(5)
2
(23)
3
(17)
4
(14)
5
(12)
6
(2)
7
(3)
8
(7)
9
(13)
10
(19)
11
(24)
12
(28)
13
(9)
14
(5)
15
(7)
16
(17)
17
(17)
18
(15)
19
(6)
20
21
(7)
22
(20)
23
(6)
24
(4)
25
(5)
26
(11)
27
(1)
28
(2)
29
(14)
30
(7)




Showing 5 results of 5

From: Timothy W. H. <hi...@me...> - 2010年11月01日 20:03:19
Hello,
I have a 2D numpy masked array of geo-located data -- with some data
missing -- that I wish to plot on a map. Basemap provides a nice tool
to do this, but I am stumped trying to get the colorscheme I want.
My data are only physically meaningful on land, so I am using
Basemap.maskoceans() to mask out "wet" locations (oceans, lakes, etc.)
Trouble is, now both water as well as actual missing data show up in
the missing data color.
I want to have blue water, some other (bright) color for missing data,
and a nice-looking color transition (matplotlib.cm.Blues or something
similar) for the valid data over land (values from 0 to 50). The
Cookbook example at
<http://www.scipy.org/Cookbook/Matplotlib/Plotting_Images_with_Special_Values>
addresses my problem, but I cannot get it to work. After changing
instances of matplotlib.numerix to numpy, I get a long list of
exceptions, the last of which is 
TypeError: __call__() got an unexpected keyword argument 'bytes'. 
This has to do with sentinelNorm, I think, but I'm not sure how to fix it.
Eventually I would like to sub-classify missing data by the type of
missing input that caused a missing value, but for now a single
missing data color is enough.
The code below does almost what I want- I just need to figure out how
to make the water blue. I have also messed around with
matplotlib.cm.BoundaryNorm to create a colormap/normalization to
handle my data, but I am getting hung up initializing the cmap,
counting bin edges, etc. I also tacked my test code for that... Any
help greatly appreciated!
Thanks,
Tim
--
Timothy W. Hilton
PhD Candidate, Department of Meteorology
The Pennsylvania State University
503 Walker Building, University Park, PA 16802
hi...@me...
#--------------------------------------------------
# amost right...
import numpy as np
import numpy.ma as ma
from mpl_toolkits.basemap import Basemap, maskoceans
import matplotlib.pyplot as plt
import matplotlib.colors
if __name__=="__main__":
 
 # setup a basemap instance & draw a map
 m_aeqd = Basemap(width=9e5,height=9e5,projection='aeqd',
 lat_0=28.46,lon_0=360-80.67, resolution='i', area_thresh=1000,
 rsphere=6371007.181000)
 col_water='#B9D3EE' #SlateGray2
 col_land ='#1C1C1C'
 m_aeqd.drawmapboundary(fill_color=col_water)
 # draw coasts and fill continents.
 m_aeqd.drawcoastlines(linewidth=0.5)
 m_aeqd.fillcontinents(color=col_land,lake_color=col_water, zorder=0)
 
 # create a 100 x 100 pseudodata array with valid data between 0
 and 50 and some "missing" data (less than zero)
 n=100
 X = ma.masked_less(np.random.random_integers(-1, 50, (n,n)), 0)
 plot_mid = np.mean((m_aeqd.llcrnrx, m_aeqd.urcrnrx))
 Xu, Xv = np.meshgrid(np.arange(stop=plot_mid + 500*n,
 start=plot_mid - 500*n, step=1000),
 np.arange(stop=m_aeqd.urcrnry,
 start=m_aeqd.urcrnry - 1000*n, step=1000))
 Xlon, Xlat = m_aeqd(Xu, Xv, inverse=True)
 #setup a colormap and plot the data
 cmap = matplotlib.cm.get_cmap("Blues", 25)
 #mask oceans
 ocean_mask = maskoceans(Xlon, Xlat, X)
 #now I'm stumped how to incorporate oceans_mask into the color map
 oceans_cmap = matplotlib.colors.ListedColormap((col_land, "#000000"),
 name="oceans", N=2)
 cmap.set_bad(color="#FF0000") #show missing vals in bright red
 m_aeqd.pcolormesh(Xu, Xv, ocean_mask, cmap=cmap)
 plt.colorbar()
 #it seems like I could do another pcolormesh call with
 #ocean_mask.mask and oceans_cmap; I'd need to set the
 #transparency. It seems like the more elegant solution is to
 #devise cmap to account for ocean_mask.mask (water) separately
 #from X.mask (data that are actually missing). I'm not sure how
 #do that though.
From: Bartosz T. <b.t...@bi...> - 2010年11月01日 19:22:32
Hi all,
Is it possible to set direction (in or out) individually for each tick. I know about the rc setting ("(x/y)tick.direction") , but I need a finer control over the ticks.
Thanks,
Bartek
From: starz1010101 <ama...@ya...> - 2010年11月01日 18:30:03
I can't force pyplot to draw in the middle a function. For example, this
function does not plot the sine before the user prompt, only after the
entire function executes:
import matplotlib.pyplot as plt
import numpy as np
def plot_now():
 plt.ion()
 plt.figure()
 x = 2*np.pi*np.linspace(0.0, 1.0, 100)
 plt.plot( x, np.sin(x) )
 plt.draw()
 plt.draw()
 q = raw_input( 'anything: ')
I'm using the enthought python distribution for mac in ipython. 
-- 
View this message in context: http://old.nabble.com/Can%27t-force-draw-tp30107286p30107286.html
Sent from the matplotlib - users mailing list archive at Nabble.com.
From: Joe K. <jki...@wi...> - 2010年11月01日 16:55:02
Hi folks,
First off, I apologize for the wall of text...
Spurred on by this Stack Overflow
question<http://stackoverflow.com/questions/4018860/text-box-in-matplotlib/4056853#4056853>,
and by an itch I've been wanting to scratch lately, I put together a a
callback function that (attempts, anyway) to auto-wrap text artists to the
boundaries of the axis they're in.
It is often useful to have text reflow/auto-wrap within the axis boundaries
during interactive use (resizing a plot with lots of labeled points, for
example...). It doesn't really need to be precise, as long as it keeps
words from being fully off the figure.
A "full" gui toolkit would be a better way of handling this, but I've gotten
in the habit of slapping a few callback functions onto matplotlib figures to
make (simple) interactive scripts that I can share across platforms. (Lab
exercises, in my case.) Having a way to make text reflow within the axis
boundaries during resizing of plots makes things a bit easier.
I'm aware that this isn't really possible in a general, backend-independent
fashion due to the subtleties of text rendering in matplotlib (e.g. things
like latex, where the length of the raw string has nothing to do with it's
rendered size, and the general fact that the size of the text isn't known
until after it's drawn). Even getting it approximately correct is still
useful, though.
I have it working about as well as the approach I'm taking can do, I think,
but I could use some help on a couple of points.
I have two specific questions, and one more general one...
First:
Is it possible to disconnect and then reconnect a callback function from an
event within the callback function, and without disconnecting all other
callback functions from the event?
I'm redrawing the canvas within a "draw_event" callback function, and I'm
currently avoiding recursion by disconnecting and reconnecting all callbacks
to the draw event. It would be nice to disconnect only the function I'm
inside, but I can't find any way of getting its cid...
Alternatively, is there a way to redraw a figure's canvas without triggering
a draw event?
Second:
Is there any way to determine the average aspect ratio of a font in
matplotlib?
I'm trying to approximate the length of a rendered text string based on it's
font size and the number of characters. Currently, I'm assuming that all
fonts have an average aspect ratio of 0.5, which works decently for most
non-monospaced fonts, but fails miserably with others.
Finally:
Is there a better way to do this? My current approach has tons of
limitations but works in most situations. My biggest problem so far is that
vertical alignment in matplotlib (quite reasonably) refers to an
axis-aligned bounding box, rather than within a text aligned bounding box.
This makes reflowing rotated text more difficult, and I'm only half-way
dealing with rotated text in the code below. Any suggestions on any points
are welcome!
Thanks!
-Joe
import matplotlib.pyplot as plt
def main():
 fig = plt.figure()
 plt.plot(range(10))
 t = "This is a really long string that I'd rather have wrapped so that
it"\
 " doesn't go outside of the figure, but if it's long enough it will go"\
 " off the top or bottom!"
 plt.text(7, 3, t, ha='center', rotation=30, va='center')
 plt.text(5, 7, t, fontsize=18, ha='center')
 plt.text(3, 0, t, family='serif', style='italic', ha='right')
 plt.title("This is a really long title that I want to have wrapped so
it"\
 " does not go outside the figure boundaries")
 fig.canvas.mpl_connect('draw_event', on_draw)
 plt.show()
def on_draw(event):
 import matplotlib as mpl
 fig = event.canvas.figure
 # Cycle through all artists in all the axes in the figure
 for ax in fig.axes:
 for artist in ax.get_children():
 # If it's a text artist, wrap it...
 if isinstance(artist, mpl.text.Text):
 autowrap_text(artist, event.renderer)
 # Temporarily disconnect any callbacks to the draw event...
 # (To avoid recursion)
 func_handles = fig.canvas.callbacks.callbacks[event.name]
 fig.canvas.callbacks.callbacks[event.name] = {}
 # Re-draw the figure..
 fig.canvas.draw()
 # Reset the draw event callbacks
 fig.canvas.callbacks.callbacks[event.name] = func_handles
def autowrap_text(textobj, renderer):
 import textwrap
 from math import sin, cos
 # Get the starting position of the text in pixels...
 x0, y0 = textobj.get_transform().transform(textobj.get_position())
 # Get the extents of the current axis in pixels...
 clip = textobj.get_axes().get_window_extent()
 # Get the amount of space in the direction of rotation to the left and
 # right of x0, y0
 # (This doesn't try to correct for different vertical alignments, and
will
 # have issues with rotated text when va & ha are not 'center')
 dx1, dx2 = x0 - clip.x0, clip.x1 - x0
 dy1, dy2 = y0 - clip.y0, clip.y1 - y0
 rotation = textobj.get_rotation()
 left_space = min(abs(dx1 / cos(rotation)), abs(dy1 / sin(rotation)))
 right_space = min(abs(dx2 / cos(rotation)), abs(dy2 / sin(rotation)))
 # Determine the width (in pixels) of the new text
 alignment = textobj.get_horizontalalignment()
 if alignment is 'left':
 new_width = right_space
 elif alignment is 'right':
 new_width = left_space
 else:
 new_width = 2 * min(left_space, right_space)
 # Estimate the width of the new size in characters...
 aspect_ratio = 0.5 # This varies with the font!!
 fontsize = textobj.get_size()
 pixels_per_char = aspect_ratio * renderer.points_to_pixels(fontsize)
 # If wrap_width is < 1, just make it 1 character
 wrap_width = max(1, new_width // pixels_per_char)
 try:
 wrapped_text = textwrap.fill(textobj.get_text(), wrap_width)
 except TypeError:
 # This appears to be a single word
 wrapped_text = textobj.get_text()
 textobj.set_text(wrapped_text)
if __name__ == '__main__':
 main()
From: Sameer G. <sam...@gm...> - 2010年11月01日 07:59:12
It's a backend issue:the code works with wxagg but not with gtkagg.
On 1 November 2010 00:54, Sameer Grover <sam...@gm...> wrote:
> Can matplotlib be used in scripts with multiple processes, such as
> when using the multiprocessing module?
>
> I had some difficulty getting code to work in which I was carrying out
> some long computations and wanted to show intermediate results in a
> separate process so as not to interrupt the main computation. The
> problem distilled down to trying to use the multiprocessing module and
> matplotlib together. This is a code snippet which I expected to work,
> but doesn't. Is this a matplotlib issue, some intricate issue with
> GUIs and processes, or something with my understanding of the subject?
>
> Would you expect this code to work? Is there any easy workaround?
>
> from pylab import *
> import multiprocessing
> class myclass(object):
>  def plotter(self):
>    plot([1,2,3,4])
>    show()
>  def mainfunction(self):
>    a = arange(1, 10, 0.1)
>    newprocess = multiprocessing.Process(target=self.plotter)
>    newprocess.start()
>    newprocess.join()
>    plot(a, sin(a))
>    show()
> if __name__== '__main__':
>  a = myclass()
>  a.mainfunction()
>
> Thanks,
> Sameer
>

Showing 5 results of 5

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.
Thanks for helping keep SourceForge clean.
X





Briefly describe the problem (required):
Upload screenshot of ad (required):
Select a file, or drag & drop file here.
Screenshot instructions:

Click URL instructions:
Right-click on the ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)

More information about our ad policies

Ad destination/click URL:

AltStyle によって変換されたページ (->オリジナル) /