#统计地图 from pyecharts import options as opts from pyecharts.charts import Map data_map=[['昆明',2338],['昭通',290],['曲靖',518],['玉溪',239],['普洱',390],['保山',288],['丽江',83],['临沧',330],['楚雄',90],['红河',296],['文山',375],['西双版纳',271],['大理',259],['德宏',295],['怒江',42],['迪庆',24]] #创建Map对象 map_demo=( Map() .add("犯罪未遂案件数量",data_map,"云南") .set_global_opts(title_opts=opts.TitleOpts(title="云南地图示例"), visualmap_opts=opts.VisualMapOpts()) ) map_demo.render_notebook # 需要引用的库 from pyecharts import options as opts from pyecharts.charts import Map # 设置不同的系列,和系列中区域对应的数量值 pair_data1= [ ['龙亭区', 100], ['顺河回族区', 200], ['鼓楼区', 300], ['禹王台区', 400], ['祥符区',500] ] pair_data2=[ ['杞县',100], ['兰考县',200], ['尉氏县',300], ['通许县',400]] def create_map(): ''' 作用:生成地图 ''' ( # 大小设置 Map() .add( series_name="开封市市区", data_pair=pair_data1, maptype="开封" ) .add( series_name="开封市县区", data_pair=pair_data2, maptype="开封" ) # 全局配置项 .set_global_opts( # 设置标题 title_opts=opts.TitleOpts(title="开封地图"), # 设置标准显示 visualmap_opts=opts.VisualMapOpts(max_=500, is_piecewise=False) ) # 系列配置项 .set_series_opts( # 标签名称显示,默认为True label_opts=opts.LabelOpts(is_show=True, color="blue") ) # 生成本地html文件 .render("省份地图.html") ) create_map() 哪里错了
时间: 2023-12-31 07:05:07 浏览: 82
代码中存在两段不同的代码,第一段是绘制云南地图的代码,第二段是绘制开封地图的代码。这两段代码之间没有任何联系,也没有错误。如果您想同时运行这两段代码,可以将它们分别放在两个不同的代码块里。同时,对于第一段代码,您需要将 `map_demo.render_notebook` 改为 `map_demo.render_notebook()`,这样才能正确地在 Jupyter Notebook 中展示地图。
相关问题
#统计地图 from pyecharts import options as opts from pyecharts.charts import Map data_map=[['昆明',2338],['昭通',290],['曲靖',518],['玉溪',239],['普洱',390],['保山',288],['丽江',83],['临沧',330],['楚雄',90],['红河',296],['文山',375],['西双版纳',271],['大理',259],['德宏',295],['怒江',42],['迪庆',24]] #创建Map对象 map_demo=( Map() .add("犯罪未遂案件数量",data_map,"云南") .set_global_opts(title_opts=opts.TitleOpts(title="云南地图示例"), visualmap_opts=opts.VisualMapOpts(min_=0, max_=3000)) ) map_demo.render_notebook() 地图没有颜色区分
你的地图没有颜色区分,可能是因为你没有在`add()`方法中指定颜色渐变。你可以为地图添加颜色渐变,来区分不同的数据范围。
例如,你可以将`add()`方法中的`data_map`参数修改为一个列表和字典的嵌套结构,将数据和颜色渐变按照一定的规则进行映射。例如:
```python
data_map = [
{"name": "昆明", "value": 2338},
{"name": "昭通", "value": 290},
{"name": "曲靖", "value": 518},
{"name": "玉溪", "value": 239},
{"name": "普洱", "value": 390},
{"name": "保山", "value": 288},
{"name": "丽江", "value": 83},
{"name": "临沧", "value": 330},
{"name": "楚雄", "value": 90},
{"name": "红河", "value": 296},
{"name": "文山", "value": 375},
{"name": "西双版纳", "value": 271},
{"name": "大理", "value": 259},
{"name": "德宏", "value": 295},
{"name": "怒江", "value": 42},
{"name": "迪庆", "value": 24},
]
map_demo = (
Map()
.add(
"犯罪未遂案件数量",
data_map,
"云南",
is_map_symbol_show=False,
label_opts=opts.LabelOpts(is_show=False),
# 指定颜色渐变
itemstyle_opts=opts.ItemStyleOpts(
color=[
"#f5ecec",
"#f5c6c6",
"#f0a4a4",
"#e88181",
"#e35e5e",
"#d93737",
"#cc1e1e",
"#b81a1a",
"#9e1616",
"#841212",
"#6a0e0e",
]
),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="云南地图示例"),
visualmap_opts=opts.VisualMapOpts(min_=0, max_=3000),
)
)
map_demo.render_notebook()
```
这里为`add()`方法中的`itemstyle_opts`参数指定了一个颜色渐变,将数据范围按照一定的规则映射到不同的颜色。你可以根据需要进一步调整颜色渐变的规则和颜色值。
from pyecharts.charts import Line from pyecharts.charts import Bar from pyecharts.charts import Pie from pyecharts.charts import Grid from pyecharts import options as opts # 柱状图 from pyecharts.charts import Bar from pyecharts import options as opts bar=Bar() bar.add_xaxis(["衬衫","毛衣","领带","裤子","风衣","高跟鞋","袜子"]) bar.add_yaxis("商家A",[120,56,28,98,129,28,107]) bar.add_yaxis("商家B",[60,140,153,145,160,70,54]) bar.set_global_opts(title_opts=opts.TitleOpts(title="商家A和商家B销售情况柱状图")) bar.render() # 折线图 from pyecharts.charts import Line from pyecharts import options as opts from pyecharts.globals import ThemeType,RenderType x_data=['1月','2月','3月','4月','5月','6月'] y_data1=[2,4.9,7,23.2,25.6,76.7] y_data2=[2.6,5.9,9,26.4,28.7,70.7] y_data3=[2,2.2,3.3,4.5,6.3,10.2] line=( Line() .add_xaxis(x_data) .add_yaxis('蒸发量',y_data1) .add_yaxis('降水量',y_data2) .add_yaxis('平均温度',y_data3) ) line.render("zhexiantu.html") # 饼图 from pyecharts.faker import Faker from pyecharts.charts import Pie import pyecharts.options as opts Faker.choose() Faker.values() list(zip(Faker.choose(),Faker.values())) pie=( Pie() .add('',[list(x) for x in zip(["衬衫","毛衣","领带","裤子","风衣","高跟鞋","袜子"],[120,56,28,98,129,28,107])],center=[450,300],radius=[0,100]) ) pie.set_global_opts(title_opts=opts.TitleOpts(title='销售情况饼图',subtitle='商家A')) pie.render() # 多图布局 grid=( Grid() .add(bar,grid_opts=opts.GridOpts(pos_left="70%")) .add(line,grid_opts=opts.GridOpts(pos_right="70%")) .add(pie,grid_opts=opts.GridOpts(pos_bottom="80%")) ) grid.render("2102010216-刘云龙.html")换成matplotlib和pandas
好的,以下是将代码转换为使用 matplotlib 和 pandas 的版本:
```python
import matplotlib.pyplot as plt
import pandas as pd
# 柱状图
df_bar = pd.DataFrame({
'商品': ["衬衫","毛衣","领带","裤子","风衣","高跟鞋","袜子"],
'商家A': [120,56,28,98,129,28,107],
'商家B': [60,140,153,145,160,70,54]
})
ax = df_bar.plot(x='商品', kind='bar')
ax.set_title('商家A和商家B销售情况柱状图')
plt.show()
# 折线图
df_line = pd.DataFrame({
'月份': ['1月','2月','3月','4月','5月','6月'],
'蒸发量': [2,4.9,7,23.2,25.6,76.7],
'降水量': [2.6,5.9,9,26.4,28.7,70.7],
'平均温度': [2,2.2,3.3,4.5,6.3,10.2]
})
ax = df_line.plot(x='月份', kind='line')
ax.set_title('蒸发量、降水量和平均温度折线图')
plt.show()
# 饼图
df_pie = pd.DataFrame({
'商品': ["衬衫","毛衣","领带","裤子","风衣","高跟鞋","袜子"],
'销售量': [120,56,28,98,129,28,107]
})
ax = df_pie.plot(kind='pie', y='销售量', labels=df_pie['商品'], autopct='%1.1f%%', startangle=90)
ax.set_title('销售情况饼图')
plt.show()
# 多图布局
fig, axs = plt.subplots(ncols=3)
# 柱状图
df_bar.plot(x='商品', kind='bar', ax=axs[0])
axs[0].set_title('商家A和商家B销售情况柱状图')
# 折线图
df_line.plot(x='月份', kind='line', ax=axs[1])
axs[1].set_title('蒸发量、降水量和平均温度折线图')
# 饼图
df_pie.plot(kind='pie', y='销售量', labels=df_pie['商品'], autopct='%1.1f%%', startangle=90, ax=axs[2])
axs[2].set_title('销售情况饼图')
plt.tight_layout()
plt.show()
```
注意:由于 pandas 的 `plot()` 函数默认使用的是 matplotlib,因此在使用 pandas 绘图时其实已经间接使用了 matplotlib。
阅读全文
相关推荐














