Terrence
Terrence

Reputation: 33

openpyxl.chart specify multiple data titles

How can one specify a title for each of the data sets using openpyxl.charts?

Using the code below makes all the data titles the same, as seen in the picture.

series = Series(xvalues, yvalues, title_from_data=False, title="X-orientation")

enter image description here

Using "title_from_data = True" unfortunately doesn't work for this instance.

Upvotes: 1

Views: 1259

Answers (1)

Terrence
Terrence

Reputation: 33

Thank you to the cheeky Charlie Clark who helped find the answer. If there are multiple data sets and "title_from_data = True" doesn't provide the correct result. Split up the data and add each to the plot separately.

  #Specifies the X values for all three charts, as they are all common.
  xvalues = Reference(WorkingSheet, min_col=1, min_row=2, max_row=20)
  
  #Specifies the y axis values for the X,Y,Z orientation of the data. 
  yvalues_x_orient = Reference(
            WorkingSheet, min_col=2, min_row=2, max_row=20
        )
  yvalues_y_orient = Reference(
            WorkingSheet, min_col=3, min_row=2, max_row=20
        )
  yvalues_z_orient = Reference(
            WorkingSheet, min_col=4, min_row=2, max_row=20
        )
        
  #Individually saves a series for X-,Y- and Z-axis into its own list.
  series_x_orient = Series(
            xvalues, yvalues_x_orient, title_from_data=False, title="X-orientation"
        )

        series_y_orient = Series(
            xvalues, yvalues_y_orient, title_from_data=False, title="Y-orientation"
        )

        series_z_orient = Series(
            xvalues, yvalues_z_orient, title_from_data=False, title="Z-orientation"
        )

        #Appends each list to the chart.
        chart.series.append(series_x_orient)
        chart.series.append(series_y_orient)
        chart.series.append(series_z_orient)

Upvotes: 1

Related Questions