pythonexcelbar-chartopenpyxl

Chart Title is in middle/between my bar graph plotted using openpyxl BarChart module


I am using openpyxl (python module) to dynamically plot a bar graph with data from respective cells, but the chart title gets in between my bar graph.

Code used to create the bar graph:

#sheet_velocity is the worksheet selected

# Chart object
chart_velocity = BarChart()

# Data and labels
chart_data = Reference(sheet_velocity, min_col=3, min_row=3, max_row=14)
categories = Reference(sheet_velocity, min_col=1, min_row=3, max_row=14)

chart_velocity.title = "Custom Title"
chart_velocity.title.text.rich.paragraphs[0].pPr = ParagraphProperties(defRPr=CharacterProperties(sz=1250))
man_layout = ManualLayout(xMode="edge", yMode="edge", x=0.0, y = -0.05) #tried -ve value
chart_velocity.title.layout = Layout(manualLayout=man_layout)
chart_velocity.add_data(chart_data, titles_from_data=False)
chart_velocity.set_categories(categories)
chart_velocity.dataLabels = DataLabelList()
chart_velocity.dataLabels.showVal = True  # Show the value of the data point
chart_velocity.dataLabels.showSerName = False
chart_velocity.dataLabels.showCatName = False
chart_velocity.dataLabels.showLegendKey = False
chart_velocity.legend = None
chart_velocity.x_axis.delete = False
chart_velocity.y_axis.delete = False
series = chart_velocity.series[0]
series.graphicalProperties.solidFill = "379e3e"
sheet_velocity.add_chart(chart_velocity, "F3")

Things that I've tried so far (have not worked):

  1. Increasing the chart.height variable

  2. Changing the position of chart title using ManualLayout

    For example: manual_layout = ManualLayout(xMode="edge", yMode="edge", x=0.0, y=0.0)

(Giving a +ve value to y would bring the title down but not move it up, tried -ve value as well --> did not work.)


Solution

  • The problem with this is how Microsoft changes Excel defaults from time to time.

    Chart title as well as axis titles and legend have overlay attributes to determine whether they overlay the other chart parts. Former Excel versions used False as default for this. Current Excel versions have True as default for this. Thus if we want not to overlay, we need set this as so:

    chart_velocity.x_axis.title.overlay = False
    chart_velocity.y_axis.title.overlay = False
    chart_velocity.title.overlay = False
    chart_velocity.legend.overlay = False
    

    Also axis titles have delete attributes to determine whether the axis shall not be displayed. This also should be set False:

    chart_velocity.x_axis.delete = False
    chart_velocity.y_axis.delete = False