PyQt5.QtChart - How to display labels in bar instead of as legends?
-
wrote on 20 Aug 2021, 22:44 last edited by Patitotective
I'm creating a
QHorizontalStackedBarSeries
and i want the legend (where are all bar sets categories, in this case: vegetables, grains, oils and fats, etc.) instead of being upside, all separate labels (of the legend) in it's respectively bar.
LikesetLabelVisible()
inQPieSeries
'sslices
:
This is my actual
QHorizontalStackedBarSeries
:
SoVegetables
will appear in the blue part and so on.
-
I'm creating a
QHorizontalStackedBarSeries
and i want the legend (where are all bar sets categories, in this case: vegetables, grains, oils and fats, etc.) instead of being upside, all separate labels (of the legend) in it's respectively bar.
LikesetLabelVisible()
inQPieSeries
'sslices
:
This is my actual
QHorizontalStackedBarSeries
:
SoVegetables
will appear in the blue part and so on.
This is want i want to achieve:
wrote on 20 Aug 2021, 23:22 last edited by@Patitotective please provide a minimal and reproducible example
-
@Patitotective please provide a minimal and reproducible example
wrote on 21 Aug 2021, 00:07 last edited by@eyllanesc here is:
import sys from PyQt5.QtWidgets import QMainWindow, QApplication from PyQt5.QtChart import QHorizontalStackedBarSeries, QBarSet, QChart, QBarCategoryAxis, QChartView from PyQt5.QtGui import QPainter class MainWindow(QMainWindow): def __init__(self, parent=None): super().__init__(parent) horizontal_stacked_bar_chart = self.create_horizontal_stacked_bar_chart() self.setCentralWidget(horizontal_stacked_bar_chart) def create_horizontal_stacked_bar_chart(self): series = QHorizontalStackedBarSeries() for food, food_value in {"Vegetables": 100, "Grains": 200, "Fruits": 150, "Protein": 50}.items(): food_set = QBarSet(food) food_set << food_value series.append(food_set) chart = QChart() chart.addSeries(series) chart.createDefaultAxes() chart.axisY(series) chart_view = QChartView(chart) chart_view.setRenderHint(QPainter.Antialiasing) return chart_view if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_())
1/3