Python Google电子表格API气泡图未正确显示

2024-10-01 00:32:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用google示例https://developers.google.com/chart/interactive/docs/gallery/bubblechart用Python v3.7.8测试google Spreadshhet API v4以创建气泡图

数据存储在谷歌硬盘的电子表格中,我在那里创建了气泡图

enter image description here

气泡图已创建,但气泡不可见/不显示。以下代码:

from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

...

body = {'requests': 
        [{'addChart': 
          {'chart': 
           {'spec': 
            {'title': 'Correlation between life expectancy, fertility rate and population of some world countries (2010)', 
             'titleTextPosition': 
             {'horizontalAlignment': 'CENTER'}, 
             'bubbleChart': 
             {'legendPosition': 'RIGHT_LEGEND',
              'domain': 
              {'sourceRange': 
               {'sources': 
                [{'sheetId': 909072886, 
                  'startRowIndex': 17, 
                  'endRowIndex': 27, 
                  'startColumnIndex': 1, 
                  'endColumnIndex': 2}]}}, 
              'series': 
              {'sourceRange': 
               {'sources': 
                [{'sheetId': 909072886, 
                  'startRowIndex': 17, 
                  'endRowIndex': 27, 
                  'startColumnIndex': 2, 
                  'endColumnIndex': 3}]}}, 
              'groupIds': 
              {'sourceRange': 
               {'sources': 
                [{'sheetId': 909072886, 
                 'startRowIndex': 17, 
                 'endRowIndex': 27, 
                 'startColumnIndex': 3, 
                 'endColumnIndex': 4}]}}, 
              'bubbleLabels': 
              {'sourceRange': 
               {'sources': 
                [{'sheetId': 909072886, 
                  'startRowIndex': 17, 
                  'endRowIndex': 27, 
                  'startColumnIndex': 0, 
                  'endColumnIndex': 1}]}}, 
              'bubbleSizes': 
              {'sourceRange': 
               {'sources': 
                [{'sheetId': 909072886, 
                  'startRowIndex': 17, 
                  'endRowIndex': 27, 
                  'startColumnIndex': 4, 
                  'endColumnIndex': 5}]}}, 
              'bubbleOpacity': 1.0}}, 
            'position': 
            {'overlayPosition': 
             {'anchorCell': 
              {'sheetId': 909072886, 
               'rowIndex': 61, 
               'columnIndex': 6}, 
               'offsetXPixels': 0, 
               'offsetYPixels': 0, 
               'widthPixels': 600, 
               'heightPixels': 371
             }
            }
           }
          }
         }
        ]
       }

response = service.spreadsheets().batchUpdate(spreadsheetId=file_id, body=body).execute()

我应该得到以下信息:

enter image description here

但我明白了:没有气泡显示。备注:将鼠标放在(不可见的)气泡上,它将显示该国所有正确的数据(预期寿命、生育率、人口、颜色正确的地区)

enter image description here

别犹豫,支持我!先谢谢你


Tags: 数据fromimportauthgooglechartbody气泡
1条回答
网友
1楼 · 发布于 2024-10-01 00:32:56

在这种情况下,我建议在请求主体中包括bubbleMaxRadiusSizebubbleMinRadiusSize,如下所示。当您的请求主体被修改时,它变成如下所示

发件人:

'bubbleOpacity': 1.0}},

致:

'bubbleOpacity': 1.0,
'bubbleMaxRadiusSize': 50,
'bubbleMinRadiusSize': 5
}},
  • 在该修改中,505分别用作bubbleMaxRadiusSizebubbleMinRadiusSize的样本值。因此,请根据您的实际情况修改这些值

结果:

enter image description here

整个脚本:

&13; 第13部分,;
body = {'requests': [{'addChart': {'chart': {'spec': {'title': 'Correlation between life expectancy, fertility rate and population of some world countries (2010)', 'titleTextPosition': {'horizontalAlignment': 'CENTER'},
     'bubbleChart': {
    'legendPosition': 'RIGHT_LEGEND',
    'domain': {'sourceRange': {'sources': [{
        'sheetId': 909072886,
        'startRowIndex': 17,
        'endRowIndex': 27,
        'startColumnIndex': 1,
        'endColumnIndex': 2,
        }]}},
    'series': {'sourceRange': {'sources': [{
        'sheetId': 909072886,
        'startRowIndex': 17,
        'endRowIndex': 27,
        'startColumnIndex': 2,
        'endColumnIndex': 3,
        }]}},
    'groupIds': {'sourceRange': {'sources': [{
        'sheetId': 909072886,
        'startRowIndex': 17,
        'endRowIndex': 27,
        'startColumnIndex': 3,
        'endColumnIndex': 4,
        }]}},
    'bubbleLabels': {'sourceRange': {'sources': [{
        'sheetId': 909072886,
        'startRowIndex': 17,
        'endRowIndex': 27,
        'startColumnIndex': 0,
        'endColumnIndex': 1,
        }]}},
    'bubbleSizes': {'sourceRange': {'sources': [{
        'sheetId': 909072886,
        'startRowIndex': 17,
        'endRowIndex': 27,
        'startColumnIndex': 4,
        'endColumnIndex': 5,
        }]}},
    'bubbleOpacity': 1.0,
    'bubbleMaxRadiusSize': 50,  # Added
    'bubbleMinRadiusSize': 5,  # Added
    }}, 'position': {'overlayPosition': {
    'anchorCell': {'sheetId': 909072886, 'rowIndex': 61, 'columnIndex': 6},
    'offsetXPixels': 0,
    'offsetYPixels': 0,
    'widthPixels': 600,
    'heightPixels': 371,
    }}}}}]}

response = service.spreadsheets().batchUpdate(spreadsheetId=file_id, body=body).execute()
和#13;
和#13;

参考文献:

相关问题 更多 >