Why Does My Pyecharts Map Lose Colors? The Hidden numpy.int64 Issue Explained
This article walks through a puzzling Pyecharts map issue where identical data produced one colored map and another blank one, reveals that the hidden cause is the use of numpy.int64 instead of plain int, and shows how casting fixes the visualization.
Hello everyone, I am a Python intermediate learner.
Preface
In a recent Python group chat, a fan asked a seemingly simple but actually tricky question about visualizing data with Pyecharts. The problem involved generating two datasets— datas and datas2 —that appeared identical, yet the resulting HTML maps looked very different.
Approach
The original code 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))
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 displays colors and data values, while the map from datas2 appears without colors or data.
Conversely, the map generated from datas2 shows no colors or data:
Solution
Inspecting the two datasets reveals that the numbers in datas are of type int, while those in datas2 are of type numpy.int64. The numpy.int64 type cannot be rendered in the HTML map, causing the visual discrepancy.
Converting the numpy.int64 values to plain int resolves the issue. The corrected function is:
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 data:
Conclusion
This article demonstrates how a subtle data type mismatch—using numpy.int64 instead of int —can break Pyecharts visualizations, and provides a simple casting fix to ensure accurate map rendering.
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.
