|
From: Bradley C. <col...@se...> - 2013-11-16 22:05:11
|
Hi there, I make a figure, then I do some stuff, then I save the figure. stuff fig = figure(figsize = (0.75, 3.5)) stuff fig.savefig(‘name.png’, bbox_inches='tight') My figure is perfect, but I'm having trouble increasing the resolution. There are two ways that I can figure out to do that, one is send a dpi as an argument to figure() and the other to savefig(). Sending it to figure() doesn’t change the file at all, so I'm not sure what it does, but sending it to savefig() does change the file. When I send dpi to savefig(), like dpi=500, the resolution seems to get much better, and the file size grows, but the problem is that the height and width of the picture grows too. I want the resolution to go up, but not the height and width of the picture. What can I do? Thanks! Bradley |
|
From: Bradley C. <col...@se...> - 2013-11-18 20:31:56
|
Chad and Alan, yes that’s the problem. Thanks for responding.
“If you like the way the image appears on screen, use the DPI argument to
savefig()”
I don’t exactly understand this. What do you mean on screen? This code is
integrated with a big project that I’m running inside of ecplise, so I’m not
sure what you mean by on screen? Either way, when I add and change a DPI
argument to savefig(), it just blows up the dimensions of the PNG.
“My question back to you is, why does it matter? Most layout programs will
let you set the physical size of the figure and scale the DPI to match.”
So you’re saying, fine let it produce a huge png and then shrink it with the
html img tag and that will do the job, right? Well, that’s really hard for
us to do because we’re using an existing solution and changing that will
involve lawyers, I kid you not.
So, I can increase the DPI on the savefig call and correspondingly shrink
the dimensions of the file in inches to get it to stay the same size, but
the problem is the text and the lines, and the axis tick marks don’t shrink
too, which is really frustrating. I can then shrink the fonts, but I don’t
know how to shrink the tick marks and the lines. Anyways, it really seems
like there should be an easier way to increase resolution – not to appear
unappreciative in any way of the hard work the devs put into this excellent
project!
Alan, I read about pixel size in that link you sent me and I have no idea
what to do. Perhaps you guys can have a look at my code?
Thanks!
Bradley
from matplotlib.pyplot import figure, cm
from numpy import arange
# Determine array sizes depending on input data set
topOfGradientColor = cm.colors.hex2color('#B5DBEF')
bottomOfGradientColor = wht = cm.colors.hex2color('#FFFFFF')
blumd = cm.colors.hex2color('#6BC3DE')
bludk = cm.colors.hex2color('#6396A5')
redmd = cm.colors.hex2color('#F7B27B')
reddk = cm.colors.hex2color('#E7754A')
gradientColorMap =
cm.colors.LinearSegmentedColormap.from_list('gradientColorMap',
[bottomOfGradientColor, topOfGradientColor], 256)
blugrd = cm.colors.LinearSegmentedColormap.from_list('blugrd',[blumd,bludk],256)
blugrd_r =
cm.colors.LinearSegmentedColormap.from_list('blugrd_r',[bludk,blumd],256)
redgrd = cm.colors.LinearSegmentedColormap.from_list('redgrd',[redmd,reddk],256)
redgrd_r =
cm.colors.LinearSegmentedColormap.from_list('redgrd_r',[reddk,redmd],256)
numYears = len(barChartData)
xMin = -0.3
xMax = numYears + 0.3
xLim = (xMin, xMax)
paddingFactor = max(abs(yMin), abs(yMax)) * 0.18
yLim = (yMin- paddingFactor, yMax + paddingFactor)
fig = figure(figsize = (numYears*0.75, 3.5))
# Determine actual plot area
subplot = fig.add_subplot(111, xlim=xLim, ylim=yLim, autoscale_on=False
,axisbg=wht)
subplot.imshow([[.7, .7],[.5,.5]], interpolation='bicubic',
cmap=gradientColorMap, \
extent=(xMin, xMax, yMin - paddingFactor, yMax + paddingFactor),
alpha=1)
# Create bars on chart
values = [tuple[1] for tuple in barChartData]
labels = ["'" + str(tuple[0])[2:] for tuple in barChartData]
xArray = arange(numYears) + 0.25
width=0.5
# Create bars and bar labels, adjusting for pos/neg values
bottom = 0
X = [[.6, .3],[.6,.3]]
for left,top in zip(xArray, values):
right = left + width
strlab = "{0:.2f}".format(top) + '%'
if top >= 0:
subplot.imshow(X, interpolation='bicubic', cmap=blugrd,
extent=(left, left+(width/2.0)+.01, bottom, top), alpha=1)
subplot.imshow(X, interpolation='bicubic', cmap=blugrd_r,
extent=(left+(width/2.0), right, bottom, top), alpha=1)
subplot.text(left+width/2., top + (paddingFactor/5), strlab,
ha='center', va='bottom', fontsize=8, family='serif')
else:
subplot.imshow(X, interpolation='bicubic', cmap=redgrd,
extent=(left, left+(width/2.0)+.01, bottom, top), alpha=1)
subplot.imshow(X, interpolation='bicubic', cmap=redgrd_r,
extent=(left+(width/2.0), right, bottom, top), alpha=1)
subplot.text(left+width/2., top - (paddingFactor/1.5), strlab,
ha='center', va='bottom', fontsize=8, family='serif')
subplot.set_xticks(xArray + width / 2) #sets x ticks
subplot.set_xticklabels(labels, fontsize=8, family='serif') # sets x labels
subplot.set_yticks([], minor=True) # minor=True means that it dynamically
assigns tick values
for lab in subplot.get_yticklabels(): # set_yticks doesn't accept font args,
so we manually set them here
lab.set_fontsize(8)
lab.set_family('serif')
subplot.hlines(0, -0.3, numYears + 0.3) # this is the horizontal line set to
zero
subplot.set_aspect('auto') # really no idea what this does.
fig.savefig(fileNameBase + '/barchart.png', bbox_inches='tight', dpi=300)
|
|
From: Alan G I. <ala...@gm...> - 2013-11-18 21:48:29
|
On 11/18/2013 3:31 PM, Bradley Coleman wrote: > when I add and change a DPI > argument to savefig(), it just blows up the dimensions of the PNG This is how I understand things, but I'm just a user. Matplotlib interprets ``dpi`` as ``pixels per inch``. PNG is a (compressed) bitmap format, so it maps to a rectangle of pixels. You are trying to set dpi, figure size in inches, and figure size in pixels independently. That's a no go. Any two will determine the 3rd. This is algebra, not a Matplotlib limitation. I ideally you will determine what you need your figure size to be **in pixels**, and then produce this when you make your figure. For example, if you want a 760x760 image, you could set the image size to say 5x5 and the dpi to 152. If you want to shrink or increase the size of a given figure, there are two ways to do it: - change dpi, which will scale everthing (including lines and fonts) - change fig size, which will leave lines and fonts the same size So if you have a target size in pixels (e.g., 760x760) and an existing figure, you have to decide whether you care about the fig size or keeping your lines and fonts the same size. However, if you are changing a bunch of figures in exactly the same fashion, you may be able to get what you want by changing the default settings to linewidth. You mentioned tick marks in particular. See the TICKS section of http://matplotlib.org/users/customizing.html (These can also be set on a per file basis, but that's a hassle.) hth, Alan Isaac |
|
From: Valentine C. J. <Jo....@na...> - 2013-11-18 21:58:41
|
Does your "existing solution" allow embedding SVG or PDF files? It sounds
to me like what you need is an image that is a higher resolution but
without changing the size it appears to be on the display. And if that's
the case, then either you need a vector image format (thus SVG or PDF, or
perhaps EPS), or otherwise you're up against the physical size of each
pixel in your display, which is well beyond the reach of any software.
Hope this helps!
-jo
On 11/18/13 3:31 PM, "Bradley Coleman" <col...@se...> wrote:
>Chad and Alan, yes that¹s the problem. Thanks for responding.
>
>³If you like the way the image appears on screen, use the DPI argument to
>savefig()²
>
>I don¹t exactly understand this. What do you mean on screen? This code
>is
>integrated with a big project that I¹m running inside of ecplise, so I¹m
>not
>sure what you mean by on screen? Either way, when I add and change a DPI
>argument to savefig(), it just blows up the dimensions of the PNG.
>
>³My question back to you is, why does it matter? Most layout programs
>will
>let you set the physical size of the figure and scale the DPI to match.²
>
>So you¹re saying, fine let it produce a huge png and then shrink it with
>the
>html img tag and that will do the job, right? Well, that¹s really hard
>for
>us to do because we¹re using an existing solution and changing that will
>involve lawyers, I kid you not.
>
>So, I can increase the DPI on the savefig call and correspondingly shrink
>the dimensions of the file in inches to get it to stay the same size, but
>the problem is the text and the lines, and the axis tick marks don¹t
>shrink
>too, which is really frustrating. I can then shrink the fonts, but I
>don¹t
>know how to shrink the tick marks and the lines. Anyways, it really seems
>like there should be an easier way to increase resolution not to appear
>unappreciative in any way of the hard work the devs put into this
>excellent
>project!
>
>Alan, I read about pixel size in that link you sent me and I have no idea
>what to do. Perhaps you guys can have a look at my code?
>
>Thanks!
>Bradley
>
>
>
>
>from matplotlib.pyplot import figure, cm
>from numpy import arange
>
># Determine array sizes depending on input data set
>topOfGradientColor = cm.colors.hex2color('#B5DBEF')
>bottomOfGradientColor = wht = cm.colors.hex2color('#FFFFFF')
>blumd = cm.colors.hex2color('#6BC3DE')
>bludk = cm.colors.hex2color('#6396A5')
>redmd = cm.colors.hex2color('#F7B27B')
>reddk = cm.colors.hex2color('#E7754A')
>gradientColorMap =
>cm.colors.LinearSegmentedColormap.from_list('gradientColorMap',
>[bottomOfGradientColor, topOfGradientColor], 256)
>blugrd =
>cm.colors.LinearSegmentedColormap.from_list('blugrd',[blumd,bludk],256)
>blugrd_r =
>cm.colors.LinearSegmentedColormap.from_list('blugrd_r',[bludk,blumd],256)
>redgrd =
>cm.colors.LinearSegmentedColormap.from_list('redgrd',[redmd,reddk],256)
>redgrd_r =
>cm.colors.LinearSegmentedColormap.from_list('redgrd_r',[reddk,redmd],256)
>
>numYears = len(barChartData)
>xMin = -0.3
>xMax = numYears + 0.3
>xLim = (xMin, xMax)
>paddingFactor = max(abs(yMin), abs(yMax)) * 0.18
>yLim = (yMin- paddingFactor, yMax + paddingFactor)
>
>fig = figure(figsize = (numYears*0.75, 3.5))
>
># Determine actual plot area
>subplot = fig.add_subplot(111, xlim=xLim, ylim=yLim, autoscale_on=False
>,axisbg=wht)
>subplot.imshow([[.7, .7],[.5,.5]], interpolation='bicubic',
>cmap=gradientColorMap, \
> extent=(xMin, xMax, yMin - paddingFactor, yMax + paddingFactor),
>alpha=1)
>
># Create bars on chart
>values = [tuple[1] for tuple in barChartData]
>labels = ["'" + str(tuple[0])[2:] for tuple in barChartData]
>
>xArray = arange(numYears) + 0.25
>width=0.5
>
># Create bars and bar labels, adjusting for pos/neg values
>bottom = 0
>X = [[.6, .3],[.6,.3]]
>for left,top in zip(xArray, values):
> right = left + width
> strlab = "{0:.2f}".format(top) + '%'
> if top >= 0:
> subplot.imshow(X, interpolation='bicubic', cmap=blugrd,
>extent=(left, left+(width/2.0)+.01, bottom, top), alpha=1)
> subplot.imshow(X, interpolation='bicubic', cmap=blugrd_r,
>extent=(left+(width/2.0), right, bottom, top), alpha=1)
> subplot.text(left+width/2., top + (paddingFactor/5), strlab,
>ha='center', va='bottom', fontsize=8, family='serif')
> else:
> subplot.imshow(X, interpolation='bicubic', cmap=redgrd,
>extent=(left, left+(width/2.0)+.01, bottom, top), alpha=1)
> subplot.imshow(X, interpolation='bicubic', cmap=redgrd_r,
>extent=(left+(width/2.0), right, bottom, top), alpha=1)
> subplot.text(left+width/2., top - (paddingFactor/1.5), strlab,
>ha='center', va='bottom', fontsize=8, family='serif')
>
>subplot.set_xticks(xArray + width / 2) #sets x ticks
>subplot.set_xticklabels(labels, fontsize=8, family='serif') # sets x
>labels
>subplot.set_yticks([], minor=True) # minor=True means that it dynamically
>assigns tick values
>for lab in subplot.get_yticklabels(): # set_yticks doesn't accept font
>args,
>so we manually set them here
> lab.set_fontsize(8)
> lab.set_family('serif')
>
>subplot.hlines(0, -0.3, numYears + 0.3) # this is the horizontal line set
>to
>zero
>
>subplot.set_aspect('auto') # really no idea what this does.
>
>fig.savefig(fileNameBase + '/barchart.png', bbox_inches='tight', dpi=300)
>
>
>
>--------------------------------------------------------------------------
>----
>Shape the Mobile Experience: Free Subscription
>Software experts and developers: Be at the forefront of tech innovation.
>Intel(R) Software Adrenaline delivers strategic insight and game-changing
>conversations that shape the rapidly evolving mobile landscape. Sign up
>now.
>http://pubads.g.doubleclick.net/gampad/clk?id=63431311&iu=/4140/ostg.clktr
>k
>_______________________________________________
>Matplotlib-users mailing list
>Mat...@li...
>https://lists.sourceforge.net/lists/listinfo/matplotlib-users
|
|
From: Chad K. <cck...@gm...> - 2013-11-17 04:38:01
|
Let me see if I understand this right, you want to leave the image dimensions, in inches, constant while increasing the resolution in DPI to get a sharper image. Did I get that correct? If you like the way the image appears on screen, use the DPI argument to savefig(). My experience is that if you change the DPI when creating the figure, that will change the layout. What it sounds like you are seeing is that when you crank the DPI up, the PNG you created does not change its listed DPI, and displays larger. I checked on my system, matplotlib 1.1.1, and it is updating the DPI in the PNG files. My question back to you is, why does it matter? Most layout programs will let you set the physical size of the figure and scale the DPI to match. On Nov 16, 2013 3:06 PM, "Bradley Coleman" <col...@se...> wrote: > Hi there, I make a figure, then I do some stuff, then I save the figure. > > stuff > fig = figure(figsize = (0.75, 3.5)) > stuff > fig.savefig(‘name.png’, bbox_inches='tight') > > My figure is perfect, but I'm having trouble increasing the resolution. > There are two ways that I can figure out to do that, one is send a dpi as > an > argument to figure() and the other to savefig(). Sending it to figure() > doesn’t change the file at all, so I'm not sure what it does, but sending > it > to savefig() does change the file. When I send dpi to savefig(), like > dpi=500, the resolution seems to get much better, and the file size grows, > but the problem is that the height and width of the picture grows too. I > want the resolution to go up, but not the height and width of the picture. > What can I do? > > Thanks! > Bradley > > > > > ------------------------------------------------------------------------------ > DreamFactory - Open Source REST & JSON Services for HTML5 & Native Apps > OAuth, Users, Roles, SQL, NoSQL, BLOB Storage and External API Access > Free app hosting. Or install the open source package on any LAMP server. > Sign up and see examples for AngularJS, jQuery, Sencha Touch and Native! > http://pubads.g.doubleclick.net/gampad/clk?id=63469471&iu=/4140/ostg.clktrk > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > |
|
From: Alan G I. <ala...@gm...> - 2013-11-17 14:03:02
|
See the discussion of pixel size at http://wiki.scipy.org/Cookbook/Matplotlib/AdjustingImageSize Alan Isaac |
|
From: Chad K. <cck...@gm...> - 2013-11-20 02:37:02
|
On 11/18/2013 12:29 PM, Coleman, Bradley wrote:
>
> Chad, yes that's the problem. Thanks for responding.
>
>
>
> "If you like the way the image appears on screen, use the DPI argument
> to savefig()"
>
>
>
> I don't exactly understand this. What do you mean on screen? This
> code is integrated with a big project that I'm running inside of
> ecplise, so I'm not sure what you mean by on screen? Either way, when
> I add and change a DPI argument to savefig(), it just blows up the
> dimensions of the PNG.
>
If you display the figure using show(), and you like how it looks, you
can ramp up the dpi for the saved figure by giving savefig() the dpi=xx
argument.
>
>
> "My question back to you is, why does it matter? Most layout programs
> will let you set the physical size of the figure and scale the DPI to
> match."
>
> * *
>
> So you're saying, fine let it produce a huge png and then shrink it
> with the html img tag and that will do the job, right? Well, that's
> really hard for us to do because we're using an existing solution and
> changing that will involve lawyers, I kid you not.
>
>
>
And I thought my change review boards were rough...
> So, I can increase the DPI on the savefig call and correspondingly
> shrink the dimensions of the file in inches to get it to stay the same
> size, but the problem is the text and the lines, and the axis tick
> marks don't shrink too, which is really frustrating. I can then
> shrink the fonts, but I don't know how to shrink the tick marks and
> the lines. Anyways, it really seems like there should be an easier
> way -- not to appear unappreciative in any way of the hard work the
> devs put into this excellent project!
>
>
>
It sounds like you may need to increase the image size in inches and
decrease the dpi to do what you want. It sounds like your html
framework or web browser is not respecting the dpi setting in the png
file that is created. As others have mentioned, can you generate an SVG
and embed that instead?
> Thanks!
>
> Bradley
>
>
>
>
>
> _
>
>
>
>
>
> *From:*Chad Kidder [mailto:cck...@gm...]
> *Sent:* Saturday, November 16, 2013 11:34 PM
> *To:* Coleman, Bradley
> *Subject:* Re: [Matplotlib-users] how can i raise the dpi without
> changing the dimensions of my picture?
>
>
>
> Let me see if I understand this right, you want to leave the image
> dimensions, in inches, constant while increasing the resolution in DPI
> to get a sharper image. Did I get that correct?
>
> If you like the way the image appears on screen, use the DPI argument
> to savefig(). My experience is that if you change the DPI when
> creating the figure, that will change the layout.
>
> What it sounds like you are seeing is that when you crank the DPI up,
> the PNG you created does not change its listed DPI, and displays
> larger. I checked on my system, matplotlib 1.1.1, and it is updating
> the DPI in the PNG files.
>
> My question back to you is, why does it matter? Most layout programs
> will let you set the physical size of the figure and scale the DPI to
> match.
>
> On Nov 16, 2013 3:06 PM, "Bradley Coleman" <col...@se...
> <mailto:col...@se...>> wrote:
>
> Hi there, I make a figure, then I do some stuff, then I save the figure.
>
> stuff
> fig = figure(figsize = (0.75, 3.5))
> stuff
> fig.savefig('name.png', bbox_inches='tight')
>
> My figure is perfect, but I'm having trouble increasing the resolution.
> There are two ways that I can figure out to do that, one is send a dpi
> as an
> argument to figure() and the other to savefig(). Sending it to figure()
> doesn't change the file at all, so I'm not sure what it does, but
> sending it
> to savefig() does change the file. When I send dpi to savefig(), like
> dpi=500, the resolution seems to get much better, and the file size grows,
> but the problem is that the height and width of the picture grows too. I
> want the resolution to go up, but not the height and width of the picture.
> What can I do?
>
> Thanks!
> Bradley
>
>
>
> ------------------------------------------------------------------------------
> DreamFactory - Open Source REST & JSON Services for HTML5 & Native Apps
> OAuth, Users, Roles, SQL, NoSQL, BLOB Storage and External API Access
> Free app hosting. Or install the open source package on any LAMP server.
> Sign up and see examples for AngularJS, jQuery, Sencha Touch and Native!
> http://pubads.g.doubleclick.net/gampad/clk?id=63469471&iu=/4140/ostg.clktrk
> _______________________________________________
> Matplotlib-users mailing list
> Mat...@li...
> <mailto:Mat...@li...>
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
|