I am trying to build slice by slice a heatmap. How can I update the data present in the graph without generating a full figure? The following code each time produces a new plot. I tried to use fig.update_traces() but it didn’t work.
What am I missing?
Thanks
import plotly.express as px
import pandas as pd
import time
df = pd.DataFrame(np.random.rand(1,100))
for i in range(0,10):
df = df.append(pd.DataFrame(np.random.rand(1,100)), ignore_index = True)
time.sleep(1)
fig = px.imshow(df)
fig.show()
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
how Animated Bar Chart Race Python : How to make a bar change its position automatically. For example, in the below code example while for countries like USA having more values, the bar should gradually move up.
import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
import numpy as np
url='https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv'
def read_file(url):
df = pd.read_csv(url)
return df
def filter_specific_country(df, selected_countries):
df1=df[df['Country/Region'].isin(selected_countries) ]
countrywise_grouped_df = df1.groupby(df['Country/Region']).sum().drop(['Lat','Long'], axis=1)
countrywise_grouped_df
return countrywise_grouped_df
def transpose_and_reformat_data(df):
df_t=df.transpose()
df_t.reset_index(inplace=True)
df_t.rename(columns={'Country/Region':'Index_Col', 'index':'Dates'}, inplace=True)
return df_t
confirmed_dataset = read_file(url)
selected_countries=['India','China','Italy','Spain','France','Australia','Germany','Japan','Korea, South','Pakistan',
'Russia','United Kingdom','Canada','Iran','Brazil','Singapore','South Africa','US']
ds=filter_specific_country(confirmed_dataset,selected_countries)
data=transpose_and_reformat_data(ds).melt(id_vars=["Dates"], var_name="Country", value_name="Confirmed_Count")
#plot_title="Global Spread of Covid-19 : (Selected Countries)"
plot_title='Visualizing the spread of Novel Coronavirus COVID-19 (2019-nCoV) - Created by Dibyendu Banerjee'
fig = px.bar(data, y="Country", x="Confirmed_Count", color="Country",
animation_frame="Dates", range_x=[1,14000000], orientation='h' )
fig.update_layout(title=plot_title,yaxis_title='Countries', xaxis_tickangle=90, font=dict(family="Arial",size=10,color="#7f7f7f"))
fig.show()
As far as I know, bar chart tracing using potly is not feasible. There is already a dedicated library that I will use to answer your question. Since the data is at the daily level, it will take a long time to play back, so I will need to resample or summarize the data into years.
from raceplotly.plots import barplot
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
url='https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv'
def read_file(url):
df = pd.read_csv(url)
return df
def filter_specific_country(df, selected_countries):
df1=df[df['Country/Region'].isin(selected_countries) ]
countrywise_grouped_df = df1.groupby(df['Country/Region']).sum().drop(['Lat','Long'], axis=1)
countrywise_grouped_df
return countrywise_grouped_df
def transpose_and_reformat_data(df):
df_t=df.transpose()
df_t.reset_index(inplace=True)
df_t.rename(columns={'Country/Region':'Index_Col', 'index':'Dates'}, inplace=True)
return df_t
confirmed_dataset = read_file(url)
selected_countries=['India','China','Italy','Spain','France','Australia','Germany','Japan','Korea, South','Pakistan',
'Russia','United Kingdom','Canada','Iran','Brazil','Singapore','South Africa','US']
ds=filter_specific_country(confirmed_dataset,selected_countries)
data=transpose_and_reformat_data(ds).melt(id_vars=["Dates"], var_name="Country", value_name="Confirmed_Count")
covid_race = barplot(data, item_column='Country', value_column='Confirmed_Count',time_column='Dates')
covid_race.plot(item_label='Top 10 crops', value_label = 'Covid Confirmed Count', date_format='%Y/%m/%d', frame_duration=800)
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
import pandas as pd
import quandl as qndl
import datetime as dt
import matplotlib.pyplot as plt
import QuandlAPIKey (My QUANDL API KEY, IGNORE THIS.)
data = qndl.get_table('AUSBS/D')
dataframe = pd.DataFrame(data)
sorteddataframe = dataframe.sort_values(by='date')
dfdate = sorteddataframe[['date']]
dfvalue = sorteddataframe[['value']]
dfx = dfdate
dfy = dfvalue
values_to_read = 100
print(sorteddataframe.head(values_to_read))
plt.plot(dfx.head(values_to_read),dfy.head(values_to_read))
plt.xlabel("Years")
plt.ylabel("Stock Values (Scaled down by 10%)")
plt.show()
I have checked the dataframes (dfx, dfy, sorteddataframe), all of them are properly sorted but the graph being generated is just a simple vertical line. Pic is posted.