Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python可视化书中代码10.4的问题 #1

Open
tianzongbo opened this issue Oct 1, 2020 · 0 comments
Open

python可视化书中代码10.4的问题 #1

tianzongbo opened this issue Oct 1, 2020 · 0 comments

Comments

@tianzongbo
Copy link

您好,我按照您提供的代码运行程序,出现了问题,请问您一下,我这里要怎么修改呢?
这是您的代码:

-- coding: utf-8 --

"""
Created on Tue Oct 15 16:56:34 2019

@author: Jie Zhang,微信公众号【EasyShu】,本代码源自《Python数据可视化之美》
"""

import geopandas as gpd
import pandas as pd
from plotnine import *

Scale=3
width=1.1

df_map = gpd.GeoDataFrame.from_file('Virtual_Map1.shp')
df_city=pd.read_csv("Virtual_City.csv")
selectCol=["orange","apple","banana","watermelon"]
MaxH=df_city.loc[:,selectCol].max().max()

df_city.loc[:,selectCol]=df_city.loc[:,selectCol]/MaxH*Scale

df_city=pd.melt(df_city.loc[:,['lat','long','group','city']+selectCol],
id_vars=['lat','long','group','city'])

df_city['hjust1']=df_city.transform(lambda x: -width if x['variable']=="orange"
else -width/2 if x['variable']=="apple"
else 0 if x['variable']=="banana" else width/2 ,axis=1)

df_city['hjust2']=df_city.transform(lambda x: -width/2 if x['variable']=="orange"
else 0 if x['variable']=="apple"
else width/2 if x['variable']=="banana" else width ,axis=1)

base_plot=(ggplot()+
geom_map(df_map,fill='white',color='gray')+
geom_rect(df_city, aes(xmin = 'long +hjust1', xmax = 'long+hjust2',
ymin = 'lat', ymax = 'lat + value' , fill= 'variable'),
size =0.25, colour ="black", alpha = 1)+
geom_text(df_city.drop_duplicates('city'),aes(x='long', y='lat', label='city'),
colour="black",size=10,nudge_y=-1.25)+
scale_fill_hue(s = 1, l = 0.65, h=0.0417,color_space='husl'))
print(base_plot)

这是报错的问题:
Traceback (most recent call last):
File "e:/Ph.D/python可视化/Beautiful-Visualization-with-python-master/第10章 地理空间型图表/图10-4-1 多数据系列的地图.py", line 27, in
else 0 if x['variable']=="banana" else width/2 ,axis=1)
File "D:\python\lib\site-packages\pandas\core\frame.py", line 7398, in transform
return self.T.transform(func, *args, **kwargs).T
File "D:\python\lib\site-packages\pandas\core\frame.py", line 7399, in transform
return super().transform(func, *args, **kwargs)
File "D:\python\lib\site-packages\pandas\core\generic.py", line 10669, in transform
raise ValueError("transforms cannot produce aggregated results")
ValueError: transforms cannot produce aggregated results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant