1

Hi Im currently wishing to label my polar bar chart in the form whereby the labels are all rotating by differing amounts so they can be read easily much like a clock. I know there is a rotation in plt.xlabel however this will only rotate it by one amount I have many values and thus would like to not have them all crossing my graph.

https://i.stack.imgur.com/PEEO3.jpg

This is figuratively what my graph is like with all the orientations in the same way, however I would like something akin to this; I really need this just using matplotlib and pandas if possible. Thanks in advance for the help!

https://i.stack.imgur.com/FO9QN.gif

Some example names might be farming, generalists, food and drink if these are not correctly rotated they will overlap the graph and be difficult to read.


from pandas import DataFrame,Series
import pandas as pd
import matplotlib.pylab as plt
from pylab import *
import numpy as np


data = pd.read_csv('/.../data.csv')

data=DataFrame(data)
N = len(data)


data1=DataFrame(data,columns=['X'])
data1=data1.get_values()

plt.figure(figsize=(8,8))
ax = plt.subplot(projection='polar')
plt.xlabel("AAs",fontsize=24)
ax.set_theta_zero_location("N")
bars = ax.bar(theta, data1,width=width, bottom=0.0,color=colours)

I would then like to label the bars according to their names which I can obtain in a list, However there are a number of values and i would like to be able to read the data names.

  • You should have a look at these... - http://stackoverflow.com/questions/21314894/setting-theta-ticks-in-matplotlib-polar-plots - http://stackoverflow.com/questions/17159668/matplotlib-adding-padding-offset-to-polar-plots-tick-labels – ssb Jun 03 '14 at 16:33
  • Yes labelling is fine like that, but if I have very long names for the points it will overlap the graph, I need one that will allow me to rotate as a function of total angle. – user3703902 Jun 04 '14 at 14:16
  • Do you have some code to show a minimal example, especially with some very long names? – CT Zhu Jun 04 '14 at 20:33

1 Answers1

0

The very meager beginnings of an answer for you (I was doing something similar, so I just threw a quick hack to go in the right direction):

# The number of labels you'd like
In [521]: N = 5

# Where on the circle it will show up
In [522]: theta = numpy.linspace(0., 2 * numpy.pi, N + 1, endpoint = True)
In [523]: theta = theta[1:]

# Create the figure
In [524]: fig = plt.figure(figsize = (6,6), facecolor = 'white', edgecolor = None)
# Create the axis, notice polar = True
In [525]: ax = plt.subplot2grid((1, 1), (0,0), polar = True)

# Create white bars so you're really just focusing on the labels
In [526]: ax.bar(theta, numpy.ones_like(theta), align = 'center', 
...:             color = 'white', edgecolor = 'white')

# Create the text you're looking to add, here I just use numbers from counter = 1 to N
In [527]: counter = 1
In [528]: for t, o in zip(theta, numpy.ones_like(theta)):
...:          ax.text(t, 1 - .1, counter, horizontalalignment = 'center', verticalalignment = 'center', rotation = t * 100)
...: counter += 1
In [529]: ax.set_yticklabels([])
In [530]: ax.set_xticklabels([])
In [531]: ax.grid(False)
In [531]: plt.show()

Rotated Numbers at the Edges

benjaminmgross
  • 2,052
  • 1
  • 24
  • 29