Why Identical Data Produces Different Pyecharts Maps and How to Fix It
This article explains why two seemingly identical datasets generate different Pyecharts maps, reveals that the discrepancy is caused by NumPy integer types, and shows how converting them to native Python ints restores the correct visual output.
Hi, I am a Python enthusiast.
Introduction
In a Python group a fan asked about a puzzling Pyecharts visualization where two datasets, datas and datas2, were identical but produced different HTML maps.
Idea
Below is the original code that reads an Excel file, creates the two datasets, and renders a map.
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Map
import operator as op
import time
df_tb = pd.read_excel('./data.xlsx')
locations = [location for location in df_tb['地区']]
values = [value for value in df_tb['2016年']]
datas = list(zip(locations, values))
print(datas)
for data in datas:
print(data)
print(type(datas))
map = (
Map()
.add('gdp', [location for location in datas], 'china')
.set_global_opts(
title_opts=opts.TitleOpts(title='各省贫困县分布图'),
visualmap_opts=opts.VisualMapOpts(max_=150)
)
)
map.render('各省贫困县分布图.html')The map generated from datas shows colors and values, while the one from datas2 appears without colors or data.
Inspecting the two lists reveals that the numbers in datas are of type int, whereas those in datas2 are numpy.int64. The latter cannot be rendered in HTML, causing the visual discrepancy.
Solution
Convert the NumPy integers to native Python ints before building the map.
def func(m):
a = []
for i in range(0, 35):
b = (df_tb['地区'][i], int(df_tb[m][i]))
a.append(b)
return aAfter applying this change and re‑running the program, the map displays correctly with colors and values.
Conclusion
The issue was caused by a data‑type mismatch; converting numpy.int64 to int resolves the problem, ensuring consistent visual output in Pyecharts maps.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
