Choropleth maps not showing but white page - python

I'm trying to make a choropleth figure and below is my sample code:
import json
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
pio.renderers.default='browser'
vietnam_state=json.load(open("diaphantinhenglish.geojson","r"))
df = pd.DataFrame({
'Name': ['Ha Noi','Ha Giang','Cao Bang','Bac Kan','Tuyen Quang'],
'Count': [3757,26,43,27,208]})
#df
Name Count
0 Ha Noi 3757
1 Ha Giang 26
2 Cao Bang 43
3 Bac Kan 27
4 Tuyen Quang 208
fig=px.choropleth(
df,
geojson=vietnam_state,
featureidkey="properties.Name",
locations="Name",
color='Count',
title="ABC",
color_continuous_scale="Aggrnyl")
fig.update_geos(fitbounds="locations",visible=False)
fig.show()
It's worked on my laptop with internet connection as below:
But on my PC without internet connection, maps doesn't show and below is console:
So I would like to ask what should I do. Thanks and best regards.

have amended your code to make it simpler to run (sources geojson and constructs a dataframe for all states)
when I switch off my internet, it still works (if there is a local copy of geojson)
is your PC plotly installation unto date? I'm using 5.3.1
it's maybe a caching issue, can you connect your PC to internet, run. Then go offline. (I have also cleared my caches, still runs)
import requests, json
import pandas as pd
import numpy as np
import plotly.express as px
from pathlib import Path
# fmt: off
f = Path().cwd().joinpath("diaphantinhenglish.geojson")
if not f.exists():
res = requests.get("https://data.opendevelopmentmekong.net/dataset/999c96d8-fae0-4b82-9a2b-e481f6f50e12/resource/2818c2c5-e9c3-440b-a9b8-3029d7298065/download/diaphantinhenglish.geojson")
with open(f, "w") as fh: json.dump(res.json(), fh)
with open(f) as fh: vietnam_state = json.load(fh)
# construct values for all states...
df = pd.DataFrame({"Name": pd.json_normalize(vietnam_state["features"]).loc[:, "properties.Name"].values}
).pipe(lambda d: d.assign(Count=np.random.randint(20, 4000, len(d))))
# fmt: on
fig = px.choropleth(
df,
geojson=vietnam_state,
featureidkey="properties.Name",
locations="Name",
color="Count",
title="ABC",
color_continuous_scale="Aggrnyl",
)
fig.update_geos(fitbounds="locations", visible=False)
fig.show()

Related

Choropleth map showing white page instead of the map

I am working on a choropleth map and it is showing a white page instead of the map as shown here
https://i.stack.imgur.com/boYKY.png
I have both the geojson and the excel file downloaded in the same folder.
geojson https://drive.google.com/file/d/1N-rp9yHqE1Rzn2VxoAAweJ8-5XIjk61j/view?usp=sharing
excel https://docs.google.com/spreadsheets/d/1NKeUg20XxJe0jccMgjj9pMxrTIIWeuQk/edit?usp=sharing&ouid=100050178655652050254&rtpof=true&sd=true
Here is my code
import json
import numpy as np
import pandas as pd
import plotly.express as px
df = pd.read_excel('kraje.xlsx', sheet_name='List1')
regions_json = json.load(open("KRAJE.geojson", "r"))
fig = px.choropleth(df,
locations="K_KRAJ",
geojson=regions_json,
color='OB1506')
fig.show()
The console of my browser in which I am viewing the map shows
this
I am using a jupyter notebook in the brave browser.
Can anyone please help me solve this? Thanks
EDIT:
I found the correct geojson file but now I have a different issue. Only one region is colored and not even in the correct color and the rest of the map even outside of my regions is colored in the same color. When I hover over my regions I can see that they are in the correct place but with a wrong color. And I also have no idea why the code colored the whole map and not only the regions from the geojson file. here is an image of the output
new (should be correct) geojson https://drive.google.com/file/d/1S03NX5Q0pqgAsbJnjqt8O5w8gUHH1rt_/view?usp=sharing
import json
import numpy as np
import pandas as pd
import plotly.express as px
df = pd.read_excel('kraje.xlsx', sheet_name='List1')
regions_json = json.load(open("KRAJE.geojson", "r"))
for feature in regions_json['features']:
feature["id"] = feature["properties"]["K_KRAJ"]
fig = px.choropleth(df,
locations="K_KRAJ",
geojson=regions_json,
color='OB1506')
fig.update_geos(fitbounds="locations", visible=False)
fig.show()
SOLUTION
Thanks to Rob Raymond it finally works. There was an issue with the geojson file. I also had a ton of problems installing geopandas and the only tutorial that actually worked was installing each package separately (https://stackoverflow.com/a/69210111/17646343)
there are multiple issues with your geojson
need to define the CRS, it's clearly not epsg:4326. Appears to be UTM CRS for Czech Republic
even with this there are invalid polygons
with valid geojson, a few points you have missed
locations needs to be common across your data frame and geojson
featureidkey needs to be used to define you are joining on name
import json
import numpy as np
import pandas as pd
import plotly.express as px
import geopandas as gpd
files = {
f.suffix: f
for p in ["KRAJE*.*", "KRAJE*.*".lower()]
for f in Path.home().joinpath("Downloads").glob(p)
}
# df = pd.read_excel('kraje.xlsx', sheet_name='List1')
df = pd.read_excel(files[".xlsx"], sheet_name="List1")
# regions_json = json.load(open("KRAJE.geojson", "r"))
regions_json = json.load(open(files[".geojson"], "r"))
regions_json = (
gpd.read_file(files[".geojson"])
.dropna()
.set_crs("EPSG:32633", allow_override=True)
.to_crs("epsg:4326")
.__geo_interface__
)
fig = px.choropleth(
df,
locations="N_KRAJ",
featureidkey="properties.name",
geojson=regions_json,
color="OB1506",
)
fig.update_geos(fitbounds="locations", visible=True)
fig
updated
there are still issues with your geojson. Have fixed it using geopandas and buffer(0) (see Fix invalid polygon in Shapely)
with this and change to plotly parameters I can now generate a figure
import json
import numpy as np
import pandas as pd
import plotly.express as px
import geopandas as gpd
from pathlib import Path
files = {
f.suffix: f
for p in ["KRAJ_*.*", "KRAJE*.*".lower()]
for f in Path.home().joinpath("Downloads").glob(p)
}
# df = pd.read_excel('kraje.xlsx', sheet_name='List1')
df = pd.read_excel(files[".xlsx"], sheet_name="List1")
# regions_json = json.load(open("KRAJE.geojson", "r"))
regions_json = json.load(open(files[".json"], "r"))
# geometry is still invalid!!! force it to valid by buffer(0)
regions_json = gpd.read_file(files[".json"]).assign(geometry=lambda d: d["geometry"].buffer(0)).__geo_interface__
fig = px.choropleth(
df,
locations="K_KRAJ",
featureidkey="properties.K_KRAJ",
geojson=regions_json,
color="OB1506",
)
fig.update_geos(fitbounds="locations", visible=True)
fig

Why is My plotly plot not displaying? I've tried show(), I've tried calling it manually, still no use?

So, this is the code, and for some reason, nothing shows up when I call fig? just a blank line, tried it with plot(),show(), still no use.
import pandas as pd
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
import plotly.express as px
import plotly.graph_objects as go
import plotly.figure_factory as ff
from plotly.subplots import make_subplots
from plotly.offline import iplot
df = pd.read_csv('covid_19_data.csv')
sns.set(rc={'figure.figsize':(8,8)})
sns.heatmap(df.isnull(),yticklabels=False)
df['ObservationDate'] = pd.to_datetime(df['ObservationDate'],format='%m/%d/%Y',utc=True)
df_grp = df.groupby('ObservationDate').agg({'Confirmed':'sum','Deaths':'sum','Recovered':'sum'})
df_grp['Active'] = df_grp['Confirmed'] -df_grp['Deaths'] - df_grp['Recovered']
df_grp = df_grp.reset_index()
fig = px.bar(df_grp,x ='ObservationDate',y = 'Confirmed',color_discrete_sequence=['red'])
this is the pic of what happens It doesn't come in the plot section as well.
Here for download is the data set under covid_19_data
https://www.kaggle.com/sudalairajkumar/novel-corona-virus-2019-dataset

Plotly Animated Bar Graph Showing 1 subgroup only in Jupyter

Issue: When I run my code only one status (sub group) shows. The data set is very simple, create date, status and count. I can only think something might be wrong with my data set at this point. Why will it only show one status of the three I have or possibly it works better with a hosted file? It seems to just iterate through the list and not keep each data point in tact until the end. The other code block works fine on github.
Sample of my data set:
Status,Create Date,Count
None,17-Apr-12,8
None,30-Apr-12,9
None,23-Aug-12,10
None,3-Oct-12,11
None,9-Jan-13,12
None,29-Jan-13,13
QBOS,31-Jan-13,1
QBDS,1-Feb-13,1
My code:
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
df = pd.read_csv('qb7.csv')
df.columns = ['Status','Create Date','Count']
includes=['None','QBDS', 'QBOS']
df=df[df['Status'].isin(includes)]
df['Create Date']= pd.to_datetime(df['Create Date']).dt.strftime('%Y-%m-%d')
fig = px.bar(df,
x="Status",
y="Count",
color="Status",
animation_frame="Create Date", hover_name="Status",
range_y=[0,8000])
fig.show()
``
Sample of what I want to make:
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
df = pd.read_csv('https://raw.githubusercontent.com/shinokada/covid-19-stats/master/data/daily-new-
confirmed-cases-of-covid-19-tests-per-case.csv')
df.columns = ['Country','Code','Date','Confirmed','Days since confirmed']
includes=['United States','Russia', 'India','Brazil']
df=df[df['Country'].isin(includes)]
df['Date']= pd.to_datetime(df['Date']).dt.strftime('%Y-%m-%d')
fig = px.bar(df, x="Country", y="Confirmed", color="Country",
animation_frame="Date", animation_group="Country", range_y=[0,35000])
fig.show()`
I think the reason it doesn't show the intended graph is because of the different number of data. The intended result is achieved when the number of data is aligned.
import pandas as pd
import numpy as np
import io
data = '''
Status,Create Date,Count
None,17-Apr-12,8
None,30-Apr-12,9
None,23-Aug-12,10
None,3-Oct-12,11
None,9-Jan-13,12
None,29-Jan-13,13
QBOS,17-Apr-12,8
QBOS,30-Apr-12,9
QBOS,23-Aug-12,10
QBOS,3-Oct-12,11
QBOS,9-Jan-13,12
QBOS,29-Jan-13,13
QBDS,17-Apr-12,8
QBDS,30-Apr-12,9
QBDS,23-Aug-12,10
QBDS,3-Oct-12,11
QBDS,9-Jan-13,12
QBDS,29-Jan-13,13
'''
df = pd.read_csv(io.StringIO(data), sep=',')
import plotly.graph_objects as go
import plotly.express as px
# df = pd.read_csv('qb7.csv')
df.columns = ['Status','Create Date','Count']
includes=['None','QBDS', 'QBOS']
df=df[df['Status'].isin(includes)]
df['Create Date']= pd.to_datetime(df['Create Date']).dt.strftime('%Y-%m-%d')
fig = px.bar(df,
x="Status",
y="Count",
color="Status",
animation_frame="Create Date", hover_name="Status",
range_y=[0,30])
fig.show()

Why is matplotlib second figure empty?

I have created a function which takes values from a CSV file into a dataframe in Python. I have automated the code so that it makes graphs of all the countries and saves it in a folder. If i pass in just one country the graph shows fine. But, when i pass in more than one country the graph appears with axes but does not show the graph lines itself. How can i fix this? Thanks in advance. Here is my code:
import pandas as pd
import numpy as np
from pandasql import sqldf
import pandasql as pdsql
import pandasql as psql
import pandas_datareader as pdr
import matplotlib.pyplot as plt
%matplotlib inline
covid=pd.read_csv("C:/Users/Test/Desktop/covid.csv")
countries = ['Canada', 'Brazil']
for country in countries:
covid = psql.sqldf(f"select co, new_deaths from covid where co= '{country}'")
plt.figure(figsize=(15,10))
plt.grid(True)
covid['MA_5'] = covid.new_deaths.rolling(5).mean()
covid['MA_10'] = covid.new_deaths.rolling(10).mean()
plt.plot(covid['new_deaths'],label='new_deaths')
plt.plot(covid['MA_5'], label='MA 5 day')
plt.plot(covid['MA_10'], label='MA 10 day')
plt.legend(loc=2)
plt.plot(country)
plt.savefig(country+".png")
plt.show()
With one countrycountries = ['Canada'] I get this:
https://i.stack.imgur.com/d6hC3.png
If i pass two countries like so: countries = ['Canada', 'Brazil'] I get this:
https://i.stack.imgur.com/d6hC3.png
https://i.stack.imgur.com/Sb6tZ.png
After countless hours of research and editing my code i have finally found the solution. You have to move the CSV code inside the for loop. Whenever you run the code, Python's memory is refreshed and it goes to the next string in the list. The graph shows fine. Here i have provided the code:
import pandas as pd
import numpy as np
from pandasql import sqldf
import pandasql as pdsql
import pandasql as psql
import pandas_datareader as pdr
import matplotlib.pyplot as plt
%matplotlib inline
countries = ['Canada', 'Brazil']
for country in countries:
covid=pd.read_csv("C:/Users/Test/Desktop/covid.csv")
covid = psql.sqldf(f"select co, new_deaths from covid where co= '{country}'")
plt.figure(figsize=(15,10))
plt.grid(True)
covid['MA_5'] = covid.new_deaths.rolling(5).mean()
covid['MA_10'] = covid.new_deaths.rolling(10).mean()
plt.plot(covid['new_deaths'],label='new_deaths')
plt.plot(covid['MA_5'], label='MA 5 day')
plt.plot(covid['MA_10'], label='MA 10 day')
plt.legend(loc=2)
plt.plot(country)
plt.savefig(country+".png")
plt.show()

Display data json (plot.ly) from influxdb

I'm trying to get a graph of my influxdb measurements.To display graphics, I use plot.ly and python. I started in python with a sql request to get my json data from localhost:8086, but it doesn't work. I've tried some things, I'm sending you my little piece of code:
import plotly
import plotly.plotly as py
import numpy as np
import plotly.graph_objs as go
import plotly.offline as ply
import pandas as pd
from plotly.tools import FigureFactory as FF
if __name__ == "__main__":
df = pd.read_json('http://localhost:8086/query?q=SELECT%20%22I%22%20FROM%20%22michelin%22.%22autogen%22.%22mqtt_consumer%22%20WHERE%20time%20%3E%20now()%20-%202d%20AND%20%22topic%22=%27PI1%27')
print(df['results'])
#df = [go.Scatter(x=df['time'], y=df['mqtt_consumer.mean_I'])]
ply.plot(df, filename = 'time-series-simple')
The link with the request shows me this :
I would like a time series, with the value "I" as a function of time.
If anyone can help me, thank you for your feedback
To plot the data via Plot.ly, You need to create a trace first where you define the X, Y axis data.
import plotly.plotly as py
import plotly.graph_objs as go
trace = go.Scatter(
x = ("Your X data, Must be a numpy array"),
y = ("Your Y data, Must be a numpy array"),
mode = 'markers'
)
data = [trace]
Then you can simply,
py.plot(data, filename='basic-line')
You must unwind all the keys before plotting. The data inside the json object that you want to plot is at the innermost nested level. You need to reach that. Try this
dfff = pd.DataFrame(df["results"][0]["series"][0]["values"]).T
#dfff.plot()
Specifically if you want to plot using plotly:
import plotly.plotly as py
import pandas as pd
import numpy as np
dfff = pd.DataFrame(df["results"][0]["series"][0]["values"]).T
dfff.columns = ["timestamp","value"]
py.iplot([{
'x': dfff.timestamp,
'y': dfff["value"],
'name': "value"
} ], filename='yourfilename')

Categories