Python Matplotlib 极坐标标注
你好,我现在想给我的极坐标条形图加标签,想让这些标签以不同的角度旋转,这样就能更容易地阅读,像时钟一样。我知道在plt.xlabel中可以旋转标签,但这只能旋转一个固定的角度,而我有很多值,所以我希望它们不会都交叉在图上。
这张图大概就是我现在的样子,所有的标签方向都是一样的,但我想要的是像这样;如果可以的话,我真的只想用matplotlib和pandas来实现。提前感谢你的帮助!
一些示例名称可能是农业、通才、食品和饮料,如果这些标签没有正确旋转,它们会重叠在图上,阅读起来会很困难。
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)
然后我想根据我可以在列表中获得的名称来给条形图加标签。不过有很多值,我希望能够清楚地读取这些数据名称。
1 个回答
0
这是一个非常简单的答案开头(我之前也在做类似的事情,所以我快速写了个小程序,帮你朝着正确的方向前进):
# 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()