问题描述
我在饼图中添加了旋转标签,并希望默认标签在饼图的每个切片中居中.但事实并非如此
如何将标签居中?
这是我的后端代码:
from PyQt5 import QtCore, QtGui, QtWidgets导入系统从PyQt5.QtWidgets导入QMainWindow,QApplication,QWidget从前端导入Ui_MainWindow从matplotlib.backends.backend_qt5agg导入FigureCanvasQTAgg作为FigureCanvas导入matplotlib.pyplot作为pltUi_MainWindow类(QMainWindow,Ui_MainWindow):def __init __(self,parent = None):超级(Ui_MainWindow,self).__ init __(parent)self.setupUi()self.graph = MyCanvas()self.gridLayout.addWidget(self.graph,0,0,1,1)self.graph.figure.clf()self.axes = self.graph.figure.add_subplot(111)self.y = [1,2,3,4,8,16,32]self.label = ['1.52%','3.03%','4.55%','6.06%','12.12%','24.24%','48.48%']self.axes.pie(self.y,labels = self.label,labeldistance = 0.6,rotatelabels = True)类 MyCanvas(FigureCanvas):def __init __(self,* args,** kwargs):self.figure = plt.figure()FigureCanvas.__init__(self, self.figure)self.figure.patch.set_facecolor(无")self.figure.subplots_adjust(left=0.08, bottom=0.10, right=0.99, top=0.97)如果 __name__ == '__main__':应用= QApplication(sys.argv)prog = Ui_MainWindow()prog.show()sys.exit(app.exec_())
看
请注意,我添加了 ax.set_aspect(1)
使圆形饼图成为圆形.如果您不想要那样,只需忽略该行.
I added rotated labels to my pie chart and expected that by default labels would be centered in each slice of the pie chart. But this is not the case
How can I center my labels?
Here my backend code:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget
from frontend import Ui_MainWindow
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
class Ui_MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Ui_MainWindow, self).__init__(parent)
self.setupUi(self)
self.graph = MyCanvas()
self.gridLayout.addWidget(self.graph, 0, 0, 1, 1)
self.graph.figure.clf()
self.axes = self.graph.figure.add_subplot(111)
self.y = [1,2,3, 4,8,16,32]
self.label = ['1.52%', '3.03%', '4.55%', '6.06%', '12.12%', '24.24%', '48.48%']
self.axes.pie(self.y, labels=self.label, labeldistance=0.6, rotatelabels =True)
class MyCanvas(FigureCanvas):
def __init__(self, *args, **kwargs):
self.figure = plt.figure()
FigureCanvas.__init__(self, self.figure)
self.figure.patch.set_facecolor("None")
self.figure.subplots_adjust(left=0.08, bottom=0.10, right=0.99, top=0.97)
if __name__ == '__main__':
app = QApplication(sys.argv)
prog = Ui_MainWindow()
prog.show()
sys.exit(app.exec_())
Looking at the documentation, you can pass dedicated options to the text objects in your pie chart using the textprops
keyword. textprops
accepts a dict
, which apparently accepts all options that are accepted by matplotlib.text.Text. Feeding it the options rotation_mode='anchor'
, va='center'
and ha='left'
gives pretty good results:
import matplotlib.pyplot as plt
figure = plt.figure()
figure.patch.set_facecolor("None")
figure.subplots_adjust(left=0.08, bottom=0.10, right=0.99, top=0.97)
figure.clf()
axes = figure.add_subplot(111)
axes.set_aspect(1)
y = [1,2,3, 4,8,16,32]
label = ['1.52%', '3.03%', '4.55%', '6.06%', '12.12%', '24.24%', '48.48%']
axes.pie(
y, labels=label, labeldistance=0.6, rotatelabels =True,
textprops = dict(rotation_mode = 'anchor', va='center', ha='left'),
)
plt.show()
The result of the code looks like this:
Note that I added ax.set_aspect(1)
to make the pie chart circular. If you don't want that, just leave out that line.
这篇关于Matplotlib饼图-如何居中放置标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!