简体   繁体   English

使用 Openpyxl python 3.7 在散点图中自定义 X 轴刻度

[英]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:我相信你的目标如下:

  • You want to add xvalues to x-axis ticks您想将xvalues添加到 x 轴刻度
  • You want to do this by creating a simple line chart?您想通过创建一个简单的折线图来做到这一点吗?

Observations:观察:

  • You are creating a 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解决方案

  1. Change chart type from ScatterChart to LineChart (or BarChart if still serves the same objective).将图表类型从ScatterChart更改为LineChart (如果仍然服务于相同的目标, ScatterChart改为BarChart )。
  2. Modify the chart build parameters修改图表构建参数

Modified Solution Code:修改后的解决方案代码:

  • LINE CHART折线图
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')

Output chart image输出图表图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM