Open
Description
Hi!
I have tried to combine a radar chart with a scatter chart to create a polar chart. When I did this I didn't manage to set the x-axis on the scatter plot to a values axis which means that I coudn't set the max and min values on the chart.
When I open the output from the script below in excel I can change the chart type to another scatter chart and a get a values x-axis. The first figure shown below is the output from python and the second is when I have changed the chart type and and made the line straight without markers.
/Jesper
#!/usr/bin/env python3
#######################################################################
#
# An example of creating a Polar chart using radar and scatter charts with Python and XlsxWriter.
#
import xlsxwriter
import numpy as np
workbook = xlsxwriter.Workbook('chart_polar.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': 1})
# Add the worksheet data that the charts will refer to.
headings = ['Degree', 'Deformation', 'x-coord','y-coord']
my_data=[
[0, 15, 30, 45, 90, 105, 120, 135, 180, 270, 350, 360],
[0, 0.1, 0.05, 0, -0.05, -0.1, -0.02, 0, 0, 0, 0.1, 0],
]
# Parameters used to set axis
max_data=max(my_data[1])
max_radar=int(max_data)+1
bas= 5*max_radar
max_axis_r = max_radar
min_axis_r = - bas
major_axis_r = (max_axis_r-min_axis_r)/6
max_axis_s = bas+max_radar
min_axis_s = -max_axis_s
# Write data to Excel sheet
worksheet.write_row('A1', headings, bold)
worksheet.write_column('A2', my_data[0])
worksheet.write_column('B2', my_data[1])
for i in range(0,len(my_data[1])):
worksheet.write_formula(i+1, 2, "=(B"+str(2+i)+"+"+str(bas)+")*SIN(A"+str(2+i)+"/180*PI())")
worksheet.write_formula(i+1, 3, "=(B"+str(2+i)+"+"+str(bas)+")*COS(A"+str(2+i)+"/180*PI())")
#######################################################################
#
# Create a new radar chart.
#
radar_chart = workbook.add_chart({'type': 'radar'})
radar_chart.add_series({
'name': ['Sheet1', 0, 8],
'categories': ['Sheet1', 1, 5, 26, 5],
'values': ['Sheet1', 1, 4, 26, 4],
'line': {'none': True},
})
#######################################################################
#
# Create a new scatter chart.
#
scatter_chart = workbook.add_chart({'type': 'scatter','subtype': 'straight'})
scatter_chart.add_series({
'name': '=Sheet1!$C$1',
'categories': '=Sheet1!$C$2:$C$13',
'values': '=Sheet1!$D$2:$D$13',
'y2_axis': True,
'x2_axis': True,
})
radar_chart.combine(scatter_chart)
# Add a chart title and some axis labels.
radar_chart.set_title ({'name': 'Polar Chart [mm]'})
radar_chart.set_y_axis({'min': min_axis_r, 'max': max_axis_r, 'major_unit': major_axis_r,})
scatter_chart.set_y2_axis({'min': min_axis_s, 'max': max_axis_s,'visible': False})
scatter_chart.set_x2_axis({'min': min_axis_s, 'max': max_axis_s,})
worksheet.insert_chart('F2', radar_chart, {'x_offset': 25, 'y_offset': 10})
workbook.close()
Activity