[Fwd: Re: Pie chart is "stretched"]

Forgot "reply-to-all".

Eric

Re: [Matplotlib-users] Pie chart is “stretched” (2.4 KB)

Thanks Eric!

Some followup questions.

-Can I give it more of a 3d look? Like the pie charts in MS Word?

-How can I reduce the size of the canvas behind the chart? There is a
lot of empty whitespace and I need to tighten up the margins as the
graph will be displayed inline on an html page...

-for the colors, do I use numbers to customize the colors, or do I
have to use colors from a built in pallet?

-lastly - is it possible to print the labels inside each slice of the
chart, instead of outside?

Thanks!
Erik

···

On 6/3/07, Eric Firing <efiring@...202...> wrote:

Forgot "reply-to-all".

Eric

---------- Forwarded message ----------
From: Eric Firing <efiring@...202...>
To: Erik Wickstrom <erik@...1629...>
Date: Sun, 03 Jun 2007 16:19:20 -1000
Subject: Re: [Matplotlib-users] Pie chart is "stretched"
Erik Wickstrom wrote:
> Hi all,
>
> I'm trying to generate a pie chart to use in my django web app. But
> it keeps rendering in a rectanuglar canvas instead of a square one, so
see inserted call to set_aspect method, below.
> the pie is oblong instead of circular. It also keeps rendering with a
> grey background, I need the background to be white.
see inserted call to set_facecolor method, below.
>
> I've attached a jpg of the chart - here is my source.
Do you really want jpeg? It is suitable for photos, but very inefficient
(and a bit fuzzy) for things like pie charts. I would use png format
instead, which is efficient for this sort of plot.

>
> def chart(request):
> from PIL import Image as PILImage
> from matplotlib.backends.backend_agg import FigureCanvasAgg as
> FigureCanvas
> from matplotlib.figure import Figure
> from StringIO import StringIO
> fig = Figure()
> canvas = FigureCanvas(fig)
> ax = fig.add_subplot(111)
> #ax.plot([1,2,3])
> labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
> fracs = [15,30,45, 10]
> ax.pie(fracs, labels=labels, shadow=True)

      ax.set_aspect('equal', adjustable='box')
      fig.set_facecolor('w')

> #ax.set_title('hi mom')
> ax.grid(True)
> #ax.set_xlabel('time')
> #ax.set_ylabel('volts')
> canvas.draw()
> size = canvas.get_renderer().get_canvas_width_height()
> buf=canvas.tostring_rgb()
> im=PILImage.fromstring('RGB', size, buf, 'raw', 'RGB', 0, 1)
> imdata=StringIO()
> im.save(imdata, format='JPEG')
> response = HttpResponse(imdata.getvalue(), mimetype='image/jpeg')
> return response
>
> Also - can I choose my own colors for the slices?

Yes, use the "colors" kwarg to specify your own list. From the docstring:

     PIE(x, explode=None, labels=None,
         colors=('b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'),
         autopct=None, pctdistance=0.6, shadow=False)

Eric

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I didn't see Eric answer your "oblong" part of the question. To make
it circular, use aspect='equal'

ax = fig.add_subplot(111, aspect='equal')

-Can I give it more of a 3d look? Like the pie charts in MS Word?

enable shadow=True, eg

pie(...., shadow=True)

-How can I reduce the size of the canvas behind the chart? There is a
lot of empty whitespace and I need to tighten up the margins as the
graph will be displayed inline on an html page...

Set your own axes dimensions, just make sure it is square

fig = figure(figsize=(6,6))
ax = fig.add_axes([0.05, 0.05, 0.9, 0.9], aspect='equal')

-for the colors, do I use numbers to customize the colors, or do I
have to use colors from a built in pallet?

You can do it either way, but if you want your colors to reflect the
magnitude of the percentage for a given slice, you will need to use a
colormap. See below

-lastly - is it possible to print the labels inside each slice of the
chart, instead of outside?

Unfortunately not, but there should be. I just added a new keyword
arg "labeldistance" which is a fraction of the radius at which to
print the label (default 1.1). Changes are in svn.

import matplotlib.cm as cm

from pylab import figure, show

# make a square figure and axes
fig = figure(figsize=(8,8))
ax = fig.add_axes([0.05, 0.05, 0.90, 0.90], aspect='equal')

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15,30,45, 10]

jet = cm.get_cmap('RdYlGn', 256)
scalarmap = cm.ScalarMappable(cmap=jet)
scalarmap.set_clim(0, 100)

colors = [scalarmap.to_rgba(frac) for frac in fracs]

ax.pie(fracs, labels=labels, colors=colors,
       pctdistance=0.4, labeldistance=0.9,
       autopct='%d%%', shadow=True)

show()

pie_demo2.png

···

On 6/4/07, Erik Wickstrom <erik@...1629...> wrote:

Hi John,

I'm already using shadow=True - is it possible to increase the size of
the shadow?

I tried fig = figure(figsize=(6,6))
ax = fig.add_axes([0.05, 0.05, 0.9, 0.9], aspect='equal')

figsize seems to change the size of the whole figure - I want a large
chart, but less whitespace behind (tighter margins).

When I use fig.add_axes() I get a grid around my pie chart.

Also - how do I change the font used in the labels? I want to use
something like Ariel.

Thanks for your help!!

Erik

···

On 6/4/07, John Hunter <jdh2358@...287...> wrote:

On 6/4/07, Erik Wickstrom <erik@...1629...> wrote:

I didn't see Eric answer your "oblong" part of the question. To make
it circular, use aspect='equal'

ax = fig.add_subplot(111, aspect='equal')

> -Can I give it more of a 3d look? Like the pie charts in MS Word?

enable shadow=True, eg

pie(...., shadow=True)

> -How can I reduce the size of the canvas behind the chart? There is a
> lot of empty whitespace and I need to tighten up the margins as the
> graph will be displayed inline on an html page...

Set your own axes dimensions, just make sure it is square

fig = figure(figsize=(6,6))
ax = fig.add_axes([0.05, 0.05, 0.9, 0.9], aspect='equal')

> -for the colors, do I use numbers to customize the colors, or do I
> have to use colors from a built in pallet?

You can do it either way, but if you want your colors to reflect the
magnitude of the percentage for a given slice, you will need to use a
colormap. See below

> -lastly - is it possible to print the labels inside each slice of the
> chart, instead of outside?

Unfortunately not, but there should be. I just added a new keyword
arg "labeldistance" which is a fraction of the radius at which to
print the label (default 1.1). Changes are in svn.

import matplotlib.cm as cm

from pylab import figure, show

# make a square figure and axes
fig = figure(figsize=(8,8))
ax = fig.add_axes([0.05, 0.05, 0.90, 0.90], aspect='equal')

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15,30,45, 10]

jet = cm.get_cmap('RdYlGn', 256)
scalarmap = cm.ScalarMappable(cmap=jet)
scalarmap.set_clim(0, 100)

colors = [scalarmap.to_rgba(frac) for frac in fracs]

ax.pie(fracs, labels=labels, colors=colors,
       pctdistance=0.4, labeldistance=0.9,
       autopct='%d%%', shadow=True)

show()