A pie chart with rotated text labels

Hi,
  I have pie charts with relatively long texts assigned to each slice of the pie.
The text is drawn horizontally. Instead, I would like to have it rotated at the
same angle as the slice itself (i.e. centered at the "axis" of the slice). In this
way the text would not overlap other text of adjacent slices (or at least if the
text starts far enough from the pie).

The example below is a bit over-twisted but I really want to be able to read at
least a portion of those ['my text4', 'my text5', 'my text6', 'my text7'] legends.

import pylab
F = pylab.gcf()
F.set_size_inches(17, 17)
ax_pie = pylab.axes([0.30, 0.30, 0.4, 0.4])
_data = [0.17, 0.23, 0.599, 0.001, 0.003, 0.003, 0.003]
_colors=['red', 'green', 'blue', 'black', 'white', 'yellow', 'violet']
_labels=['my text1', 'my text2', 'my text3', 'my text4', 'my text5', 'my text6', 'my text7']
ax_pie.pie(_data, colors=_colors, labels=_labels, labeldistance=1.5, autopct='%1.1f%%')
pylab.axis('equal')
pylab.title("blah")
pylab.show()

  I tried to search for some example of this or documentation of the pie function
but it seems it is either not possible or not documented. :wink:

Some relatively close matches I found:

http://matplotlib.sourceforge.net/examples/pylab_examples/text_rotation.html (this
could maybe help but I do not know at what angle is each slice rendered by pylab.pie())

http://matplotlib.sourceforge.net/examples/pylab_examples/text_rotation_relative_to_line.html
(maybe this would work if all text should be drawn at a SAME angle but that is not what I want)

Some other shots:
http://stackoverflow.com/questions/9220933/plotting-a-pie-chart-in-matplotlib-at-a-specific-angle-with-the-fracs-on-the-wed

http://guutaranoheya.web.fc2.com/math/matplotlib_ex.html (search for "rotation")

Thank you for your help,
Martin

Hmmm, this might be a decent feature to add to pie(). Although, I wonder if a legend would better suit your needs?

Ben Root

···

On Monday, September 24, 2012, Martin Mokrejs wrote:

Hi,

I have pie charts with relatively long texts assigned to each slice of the pie.

The text is drawn horizontally. Instead, I would like to have it rotated at the

same angle as the slice itself (i.e. centered at the “axis” of the slice). In this

way the text would not overlap other text of adjacent slices (or at least if the

text starts far enough from the pie).

The example below is a bit over-twisted but I really want to be able to read at

least a portion of those [‘my text4’, ‘my text5’, ‘my text6’, ‘my text7’] legends.

Hi Ben,

Benjamin Root wrote:

    Hi,
      I have pie charts with relatively long texts assigned to each slice of the pie.
    The text is drawn horizontally. Instead, I would like to have it rotated at the
    same angle as the slice itself (i.e. centered at the "axis" of the slice). In this
    way the text would not overlap other text of adjacent slices (or at least if the
    text starts far enough from the pie).

    The example below is a bit over-twisted but I really want to be able to read at
    least a portion of those ['my text4', 'my text5', 'my text6', 'my text7'] legends.

Hmmm, this might be a decent feature to add to pie(). Although, I wonder if a legend would better suit your needs?

The problem is that some of my pie charts have dozens of slices or very varying width.
The legend would take just too much space and moreover, there is not that many colors
easily distinguishable by eye so a person would have a hard time to find which item in
the legend corresponds to some slice in the chart.

I think this is the only way out. :wink:

Martin
BTW: A percentage in black color on a blue slice is hardly readable. Could pie() also
change a font foreground color if the background is too dark in those few slices? Say
to white? ;-))

···

On Monday, September 24, 2012, Martin Mokrejs wrote: