[英]Customised X-axis ticks in Scatter Plot using Openpyxl python 3.7
I am trying to add customized x-axis ticks from particular column values, similar to set_categories
of a pie-chart.我正在尝试从特定列值添加自定义的 x 轴刻度,类似于饼图的
set_categories
。 I have gone through the documentation and failed to find any solution.我已经浏览了文档,但没有找到任何解决方案。 Below is my code:
下面是我的代码:
from openpyxl.chart import ScatterChart, Reference, Series
wb = openpyxl.Workbook()
sheet = wb.active
rows = [
("Number", "Sales", "Market") ,
("Number of Products", "Sales in USD", "Market share"),
('0x1', 12200, 15),
('0x2', 60000, 33),
('0x3', 24400, 10),
('0x4', 32000, 42),
]
for row in rows:
sheet.append(row)
chart = ScatterChart()
xvalues = Reference(sheet, min_col = 1, min_row = 3, max_row = 6)
yvalues = Reference(sheet, min_col = 2, min_row = 3, max_row = 6)
size = Reference(sheet, min_col = 3, min_row = 3, max_row = 6)
series = Series(values = yvalues, xvalues =xvalues , zvalues =size , title ="2013")
chart.series.append(series)
chart.title = " SCATTER-CHART "
chart.x_axis.title = " X_AXIS "
chart.y_axis.title = " Y_AXIS "
sheet.add_chart(chart, "E2")
wb.save(" ScatterChart.xlsx")
Can any one tell me how to add xvalues
to x-axis ticks.任何人都可以告诉我如何将
xvalues
添加到 x 轴刻度。 This is the expected graph I am looking for:这是我正在寻找的预期图表:
I believe your goal as follows:我相信你的目标如下:
xvalues
to x-axis ticksxvalues
添加到 x 轴刻度 Observations:观察:
ScatterChart
and trying to set x-axis ticks using string labels, which is not supported by this chart type, only numeric values are supported.ScatterChart
并尝试使用字符串标签设置 x 轴刻度,此图表类型不支持,仅支持数值。 Solution解决方案
ScatterChart
to LineChart
(or BarChart
if still serves the same objective).ScatterChart
更改为LineChart
(如果仍然服务于相同的目标, ScatterChart
改为BarChart
)。Modified Solution Code:修改后的解决方案代码:
from openpyxl import Workbook
from openpyxl.chart import LineChart
wb = Workbook()
sheet = wb.active
rows = [
("Number", "Sales", "Market") ,
("Number of Products", "Sales in USD", "Market share"),
('0x1', 12200, 15),
('0x2', 60000, 33),
('0x3', 24400, 10),
('0x4', 32000, 42),
]
for row in rows:
sheet.append(row)
# build chart
chart = LineChart()
# label chart
chart.title = " SCATTER-CHART "
chart.x_axis.title = " X_AXIS "
chart.y_axis.title = " Y_AXIS "
# define data series and domain range
xvalues = Reference(sheet, min_col = 1, min_row = 3, max_row = 6)
yvalues = Reference(sheet, min_col = 2, min_row = 3, max_row = 6)
# add data to chart
chart.add_data(yvalues)
chart.set_categories(xvalues)
sheet.add_chart(chart, 'E2')
# save file
wb.save('LineChart.xlsx')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.