there seems to be an issue with my code. My goal is to plot a map that represents an outcome (population) accross the regions of Benin.
import pandas as pd
import matplotlib as mpl
database_path = "datafinalproject.csv"
database = pd.read_csv(database_path)
#Creating a geodataframe
points = gpd.points_from_xy(database["longitude"], database["latitude"], crs="EPSG:4326")
map = gpd.GeoDataFrame (database, geometry=points)
I get this message when I type map.plot and I when I type map.plot(column='population'), I get an empty map.
Can you help me solve this problem?
database.head() gives :
map.plot() will work in a Jupyter notebook but not in a normal Python environment.
You should import matplotlib.pyplot and add plt.show() at the end of your code:
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
database_path = "datafinalproject.csv"
database = pd.read_csv(database_path)
#Creating a geodataframe
points = gpd.points_from_xy(database["longitude"], database["latitude"], crs="EPSG:4326")
map = gpd.GeoDataFrame (database, geometry=points)
map.plot()
plt.show()
Related
I am trying to connect lines based on a specific relationship associated with the points. In this example the lines would connect the players by which court they played in. I can create the basic structure but haven't figured out a reasonably simple way to create this added feature.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df_dict={'court':[1,1,2,2,3,3,4,4],
'player':['Bob','Ian','Bob','Ian','Bob','Ian','Ian','Bob'],
'score':[6,8,12,15,8,16,11,13],
'win':['no','yes','no','yes','no','yes','no','yes']}
df=pd.DataFrame.from_dict(df_dict)
ax = sns.boxplot(x='score',y='player',data=df)
ax = sns.swarmplot(x='score',y='player',hue='win',data=df,s=10,palette=['red','green'])
plt.show()
This code generates the following plot minus the gray lines that I am after.
You can use lineplot here:
sns.lineplot(
data=df, x="score", y="player", units="court",
color=".7", estimator=None
)
The player name is converted to an integer as a flag, which is used as the value of the y-axis, and a loop process is applied to each position on the court to draw a line.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df_dict={'court':[1,1,2,2,3,3,4,4],
'player':['Bob','Ian','Bob','Ian','Bob','Ian','Ian','Bob'],
'score':[6,8,12,15,8,16,11,13],
'win':['no','yes','no','yes','no','yes','no','yes']}
df=pd.DataFrame.from_dict(df_dict)
ax = sns.boxplot(x='score',y='player',data=df)
ax = sns.swarmplot(x='score',y='player',hue='win',data=df,s=10,palette=['red','green'])
df['flg'] = df['player'].apply(lambda x: 0 if x == 'Bob' else 1)
for i in df.court.unique():
dfq = df.query('court == #i').reset_index()
ax.plot(dfq['score'], dfq['flg'], 'g-')
plt.show()
To create a Voronoi polygon with geovoronoi lib i use:
polyShapes, puntos = voronoi_regions_from_coords(coords, milagroShape)
coords is a geoDataFrame object that it contains map´s locations and milagroShape is a polygon.shp. Now, to plot the Voronoi use the code:
fig, ax = subplot_for_map(figsize=[14, 8])
plot_voronoi_polys_with_points_in_area(ax, milagroShape, polyShapes, coords, puntos)
ax.set_title('Diagrama de Voronoi')
plt.tight_layout()
plt.show()
Now it works, the graph is showed on screen, but it´s only a mathplotlib plot.
I guess that I have to convert it into a geodataframe object (to that, I use geopandas library).
This is the map where I need to put the Voronoi graph:
Only the polygon of the city´s area is set, but I want to set the Voronoi too.
To add the city´s area I used the code below:
for _, r in milagro.iterrows(): #milagro is a geodataframe object
#sim_geo = gpd.GeoSeries(r['geometry'])
sim_geo = gpd.GeoSeries(r['geometry']).simplify(tolerance=0.0001)
geo_j = sim_geo.to_json()
geo_j = folium.GeoJson(data=geo_j,
style_function=lambda x: {'fillColor': 'orange'})
#folium.Popup(r['Name']).add_to(geo_j)
geo_j.add_to(mapaMilagro) #mapaMilagro is a folium map object
Libraries that i use for my proyect are:
import folium #map library
import pandas as pd #Data Frame
import matplotlib.pyplot as plt #to plot graphs
import condacolab #To install some libraries
import geopandas as gpd #Geo Data Frame library
from shapely.ops import cascaded_union #I don´t know what is this xd
from geovoronoi.plotting import subplot_for_map, plot_voronoi_polys_with_points_in_area
from geovoronoi import voronoi_regions_from_coords, points_to_coords
polyShapes, puntos = voronoi_regions_from_coords(coords, milagroShape)
polyShapes is a dict where the keys are meaningless (?) numbers and the values are shapely polygons. You can load those into a new gpd dataframe.
I am working on Kaggle Global Terrorism Database (https://www.kaggle.com/START-UMD/gtd/download) and I am trying to use geopandas for visualization.
I am also using countries dataset (http://www.naturalearthdata.com/downloads/110m-cultural-vectors/110m-admin-0-countries/)
import seaborn as sns
import geopandas as gpd
import matplotlib.pyplot as plt
sns.set(style = "ticks", context = "poster")
from shapely.geometry import Point
countries = gpd.read_file("C:/Users/petr7/Desktop/ne_110m_admin_0_countries/")
countries = countries[(countries['NAME'] != "Antarctica")]
countries.plot(figsize = (15, 15))
using code above I can easily plot entire Europe,
after that I import kaggle terrorist dataset and define it as geopandas dataframe
DF = pd.read_csv("C:/Users/petr7/Desktop/gtd/globalterrorismdb_0718dist.csv", encoding='latin1')
crs = {"init": "epsg:4326"}
geometry = [Point(xy) for xy in zip ( DF["longitude"], DF["latitude"])]
geo_DF = gpd.GeoDataFrame(DF, geometry = geometry)
geo_DF.head()
Until this point everything is working and dataset can be inspect
NOW when I try to plot it it return nonsense plot:
geo_DF.plot()
I am prety new to geopandas so I wanted to ask what I am missing and also how would you plot entire europe map (countries.plot) and above that terrorist attacks?
PICTURE HERE
There is an error in the data. DF["longitude"].min() gives -86185896.0.
DF.loc[DF["longitude"] == DF["longitude"].min()]
As you can see if you run the snippet above, row with the error is 17658.
It seems to be missing comma. If you do
DF.at[17658, 'longitude'] = -86.185896
before generating geometry, it will work. Or you can drop the row if you are not sure what is exactly wrong with the data.
I have a graph, and I would like to make one of my lines different color
Tried using the matplotlib recommendation which just made me print two graphs
import numpy as np
import pandas as pd
import seaborn as sns
data = pd.read_csv("C:\\Users\\Nathan\\Downloads\\markouts_changed_maskedNEW.csv");
data.columns = ["Yeet","Yeet1","Yeet 2","Yeet 3","Yeet 4","Yeet 7","Exchange 5","Yeet Average","Intelligent Yeet"];
mpg = data[data.columns]
mpg.plot(color='green', linewidth=2.5)
I am trying to create a choropleth map using basemap and pandas, to plot the level of prescription rates across CCGs (NHS Clinical Commissioning Groups). I am downloading the shapefile from http://geoportal.statistics.gov.uk/datasets/1bc1e6a77cdd4b3a9a0458b64af1ade4_1 which provides the CCG area boundaries.. However the initial problem I am encountering is to do with the reading of the shapefile.
The following error is arising:
raise IOError('cannot locate %s.shp'%shapefile)
This is my code so far...
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.colors import Normalize
fig, ax = plt.subplots(figsize=(10,20))
m = Basemap(resolution='c', # c, l, i, h, f or None
projection='merc',
lat_0=54.5, lon_0=-4.36,
llcrnrlon=-6., llcrnrlat= 49.5, urcrnrlon=2., urcrnrlat=55.2)
m.drawmapboundary(fill_color='#46bcec')
m.fillcontinents(color='#f2f2f2',lake_color='#46bcec')
m.drawcoastlines()
m.readshapefile('/Volumes/Clinical_Commissioning_Groups_April_2016_Full_Extent_Boundaries_in_England', 'areas', drawbounds =True)
m.areas
df_poly = pd.DataFrame({'shapes': [Polygon(np.array(shape), True) for shape in m.areas],'area': [area['ccg16cd'] for area in m.areas_info]})
rates=pd.read_csv('Volumes/TOSHIBA EXT/Basemap rates.csv', delimiter=",", usecols=[0,6])
rates.columns = ['ccg16cd','MEAN YEARLY PRESCRIPTION RATE']
frame = df_poly.merge(rates, on='ccg16cd', how='left')
cmap = plt.get_cmap('Oranges')
pc = PatchCollection(df_poly.shapes, zorder=2)
norm = Normalize()
pc.set_facecolor(cmap(norm(df_poly['count'].fillna(0).values)))
ax.add_collection(pc)
mapper = matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap)
mapper.set_array(df_poly['count'])
plt.colorbar(mapper, shrink=0.4)
m
Would appreciate any pointers as to how I can achieve this choropleth map - starting with what is going wrong in reading the shapefile.
Try using geopandas to read in the shapefile:
import geopandas as gp
shape_file = gp.read_file('FileName.shp')
Also, check that the path to the shapefile is correct.