I try to display a map using json but this is the first time and I am probably using it wrong because when using px.choropleth alone my map is showing.
But I would like to use px.choropleth mapbox for a more elaborate map.
Here is the code below
Thanks for your help
import pandas as pd
import matplotlib
import folium
import matplotlib.pyplot as plt
import plotly.graph_objects as go
import plotly.express as px
from plotly.offline import plot
import json
import plotly.io as pio
Final = pd.read_excel('/Users/Desktop/Data_Extract_From_Indicateurs_du_développement_dans_le_monde (3).xlsx')
DataSet.head()
worldmap = json.load(open("/Users/Desktop/countries.geojson",'r'))
worldmap['features'][1]['properties']
world_id_map = {}
for feature in worldmap['features']:
feature['id'] = feature['properties']['ADMIN']
world_id_map[feature['properties']['ISO_A3']] = feature['id']
world_id_map = json.loads(worldmap)
figmap = px.choropleth(Final,
locations='Country Code',
color='CO2 emissions',
color_continuous_scale="Algae",
animation_frame='Date',
range_color=[20,10],
title='Worldwilde CO2 Emissions per habitant',
)
plot(figmap)
fig = go.Figure(go.Choroplethmapbox(geojson=world_id_map,
locations=Final['Country Code'],
z=Final['CO2 emissions'],
colorscale='algae', zmin=0, zmax=35,
colorbar_title = "CO2 emissions",
marker_opacity=0.5, marker_line_width=0.2))
fig.update_geos(fitbounds="locations", visible=True)
fig.update_layout(mapbox_style="carto-positron")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
plot(fig)
have simulated your data frame and dynamically sourced heojson from GitHub
there is not need to manipulate the geojson all you need to do is pass the featureidkey parameter to link to locations
have left code in place for producing choropleth and choropleth_mapbox
import pandas as pd
import plotly.express as px
from plotly.offline import plot
import plotly.io as pio
import requests
import numpy as np
# Final = pd.read_excel('/Users/Desktop/Data_Extract_From_Indicateurs_du_développement_dans_le_monde (3).xlsx')
# DataSet.head()
# worldmap = json.load(open("/Users/Desktop/countries.geojson",'r'))
# get countries from internet...
worldmap = requests.get(
"https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_110m_admin_0_countries.geojson"
).json()
# simulate dataframe, don't to local file
Final = pd.DataFrame(
{
"Country Code": [f["properties"]["ADM0_A3"] for f in worldmap["features"]],
"Date": np.tile(
pd.date_range("1-jan-2015", freq="Y", periods=7).strftime("%Y-%m-%d"), 100
)[0 : len(worldmap["features"])],
"CO2 emissions": np.random.uniform(10, 20, len(worldmap["features"])),
}
)
figmap = px.choropleth(
Final,
locations="Country Code",
color="CO2 emissions",
color_continuous_scale="Algae",
animation_frame="Date",
range_color=[20, 10],
title="Worldwilde CO2 Emissions per habitant",
)
figmap = px.choropleth_mapbox(
Final,
locations="Country Code",
geojson=worldmap,
featureidkey="properties.ADM0_A3",
color="CO2 emissions",
color_continuous_scale="Algae",
animation_frame="Date",
range_color=[20, 10],
title="Worldwilde CO2 Emissions per habitant",
mapbox_style="carto-positron",
).update_layout(mapbox={"zoom":2})
plot(figmap)
Related
I hacked together this code to plot lat and lon coordinates on a map, and the code works pretty darn well, but I can't seem to get the legend displayed, so it's hard to tell what I'm actually looking at.
import pandas as pd
import pandas_bokeh
import matplotlib.pyplot as plt
import pgeocode
import geopandas as gpd
from shapely.geometry import Point
from geopandas import GeoDataFrame
pandas_bokeh.output_notebook()
import plotly.graph_objects as go
nomi = pgeocode.Nominatim('us')
df_melted['Latitude'] = (nomi.query_postal_code(df_melted['my_zip'].tolist()).latitude)
df_melted['Longitude'] = (nomi.query_postal_code(df_melted['my_zip'].tolist()).longitude)
df_melted['colors'] = df_melted['value'].groupby(df_melted['value']).transform('count')
print(df_melted.shape)
print(df_melted.head())
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
fig = go.Figure(data=go.Scattergeo(
lon = df_melted['Longitude'],
lat = df_melted['Latitude'],
text = df_melted['value'],
marker_color = df_melted['colors']
))
fig.update_layout(
autosize=False,
width=1000,
height=1000,
title = 'Footprints Compared Based on Lat & Lon Coordinates)',
geo_scope='usa',
showlegend=True
)
fig.update_layout(legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1
))
fig.show()
When I run the code, I see a nice map of the US, but there is not legend, even though I'm using this small script directly below, which came straight from the Plotly documentation.
legend=True & showlegend=True
Both gave me errors. Any idea how to get the legend to show up here?
have used earthquake data to be able to simulate df_melted with compatible columns
there really is only one missing parameter: marker_coloraxis="coloraxis"
also changed showlegend=False
full working example using OP plotting code
import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd
from shapely.geometry import Point
from geopandas import GeoDataFrame
import plotly.graph_objects as go
import requests
res = requests.get(
"https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojson"
)
us = (
gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
.loc[lambda d: d["iso_a3"].eq("USA"), "geometry"]
.values[0]
)
gdf = gpd.GeoDataFrame.from_features(res.json(), crs="epsg:4386").loc[
lambda d: d.intersects(us)
]
df_melted = pd.DataFrame(
{
"Latitude": gdf["geometry"].y,
"Longitude": gdf["geometry"].x,
"colors": gdf["mag"],
"value": gdf["place"],
}
)
fig = go.Figure(
data=go.Scattergeo(
lon=df_melted["Longitude"],
lat=df_melted["Latitude"],
text=df_melted["value"],
marker_color=df_melted["colors"],
marker_coloraxis="coloraxis",
)
)
fig.update_layout(
autosize=False,
width=400,
height=400,
title="Footprints Compared Based on Lat & Lon Coordinates)",
geo_scope="usa",
showlegend=False,
)
fig.update_layout(
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
)
fig.show()
I wish to plot a choropleth of count (frequency) and regions (using regional ISO codes) in US. I'm using plotly currently but I can't seem to figure why the colors which should show frequency isn't been displayed.
I haven't got fibs column, all I've got is ISO.
Thanks..
from urllib.request import urlopen
import pandas as pd
import plotly.express as px
import json
from pyspark.sql.types import IntegerType
# with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as # response:
# counties = json.load(response)
df = location_test.toPandas()
fig = px.choropleth_mapbox(df, locations=df['location_region_iso_code'], color=df['count'],
color_continuous_scale="Viridis",
range_color=(0, 15),
mapbox_style="carto-positron",
zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
opacity=0.5,
labels={'count':'Count'}
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
This is the output I get:
I tried the following code:
import plotly.io as pio
import plotly.express as px
import json
import pandas as pd
import plotly.graph_objects as go
import plotly.figure_factory as ff
import plotly.express as px
df = px.data.carshare()
fig = go.Figure()
app = dash.Dash()
#fac figurile
fig = ff.create_hexbin_mapbox(df,lat = 'centroid_lat', lon = 'centroid_lon',nx_hexagon = 10,color = 'car_hours',
labels = {'color':'Point Count '},
opacity = 0.5)
fig.update_layout(mapbox_style="carto-darkmatter")
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))
fig.show()
And it displays:
And I want to modify the hover so that it will only show me the float value with only the first decimal on hover and I also want to be able to display something after it displays the value. For example the value on the hover should be 'Point Count = 1019.9 cars per hour. Unfortunately, the documentation does not help very much.
It seems to me that your best option for ff.create_hexbin_mapbox would be to configure it directly through:
fig.data[0].hovertemplate = 'Point Count =%{z:,.1f}<extra>Cars per hour</extra>'
Which will turn this:
... into this:
Complete code
import plotly.io as pio
import plotly.express as px
import json
import pandas as pd
import plotly.graph_objects as go
import plotly.figure_factory as ff
import plotly.express as px
df = px.data.carshare()
fig = go.Figure()
# app = dash.Dash()
#fac figurile
fig = ff.create_hexbin_mapbox(df,lat = 'centroid_lat', lon = 'centroid_lon',nx_hexagon = 10,color = 'car_hours',
labels = {'color':'Point Count '},
opacity = 0.5)
fig.update_layout(mapbox_style="carto-darkmatter")
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))
fig.data[0].hovertemplate = 'Point Count =%{z:,.1f}<extra>Cars per hour</extra>'
fig.show()
I have a csv file with the following structure:
I wrote this code:
import pandas as pd
import plotly.express as px
input_file = "inf.csv"
df = pd.read_csv(input_file)
fig = px.bar(df,
x='Date',
y='User',
title='Test',
color='Items',
barmode='stack')
fig.show()
and this is the output:
I would like to put on Y axis not the Users, but a number which counts how many users exists in the same day.
How can I do that?
You can get your desired data structure using df.groupby('Date').count().reset_index().
Plot:
Code:
import pandas as pd
from plotly.subplots import make_subplots
import plotly.express as px
import plotly.graph_objs as go
import plotly.io as pio
#pio.renderers.default = 'jupyterlab'
# Sample data
d={'Date': ['01/08/2019', '01/08/2019', '07/08/2019', '12/08/2019',
'26/08/2019', '29/08/2019', '29/08/2019'],
'User':['U1', 'U2', 'U3', 'U4', 'U5', 'U6', 'U7'],
'Items': ['Pen', 'Ruler', 'Rubber', 'Rubber', 'Ruler', 'Ruler', 'Pen']
}
# data strucutre
df=pd.DataFrame(d)
dfg=df.groupby('Date').count().reset_index()
dfg=dfg.rename(columns={"User": "Users"})
# plot structure
fig = px.bar(dfg,
x='Date',
y='Users',
title='Test',
#color='Items',
barmode='stack')
# plot
fig.show()
I get different results when trying to plot the identical data with mathplotlib and plotly. Plotly doesn't show me the whole data range.
import plotly.plotly as py
import plotly.graph_objs as go
# filter the data
df3 = df[df.line_item_returned==0][['created_at', 'line_item_price']].copy()
# remove the time part from datetime
df3.created_at = df3.created_at.dt.floor('d')
# set the datatime column as index
df3 = df3.set_index('created_at')
# Create traces
trace0 = go.Scatter(
x = df3.index,
y = df3.line_item_price.resample('d').sum().rolling(90, center=True).mean(),
mode = 'markers',
name = 'markers'
)
data = [trace0]
py.iplot(data, filename='scatter-mode')
The chart shows only the range Oct-Dec 2018.
Plotting the same data with matplotlib shows the whole data range 2016-2018:
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(df3.line_item_price.resample('d').sum().rolling(90, center=True).mean())
The index contains all data 2016-2018:
df3.line_item_price.resample('d').sum().rolling(31, center=True).mean().index
DatetimeIndex(['2015-11-18', '2015-11-19', '2015-11-20', '2015-11-21',
'2015-11-22', '2015-11-23', '2015-11-24', '2015-11-25',
'2015-11-26', '2015-11-27',
...
'2018-12-10', '2018-12-11', '2018-12-12', '2018-12-13',
'2018-12-14', '2018-12-15', '2018-12-16', '2018-12-17',
'2018-12-18', '2018-12-19'],
dtype='datetime64[ns]', name='created_at', length=1128, freq='D')
Why is this happening?
I guess it's a problem with indices.
%matplotlib inline
import plotly.offline as py
import plotly.graph_objs as go
import pandas as pd
import numpy as np
N = 2000
df = pd.DataFrame({"value":np.random.randn(N)},
index=pd.date_range(start='2015-01-01', periods=N))
# you don't really need to us `plt`
df.resample('d').sum().rolling(90, center=True).mean().plot();
But then if you want to use plotly you should use the index from the resampled Series.
df_rsmpl = df.resample('d').sum().rolling(90, center=True).mean()
trace0 = go.Scatter(x = df_rsmpl.index,
y = df_rsmpl["value"])
data = [trace0]
py.iplot(data)