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()
Related
I have the following example. I can change the fill color to 'green' following the documentation but I cannot find how to change the line/dots and tooltip colours to the same one. What's the best way to change all into one colour?
import numpy as np
import pandas as pd
import plotly.express as px
df = px.data.wind()
df_test = df[df["strength"]=='0-1']
fig.data=[]
fig.add_trace(go.Scatterpolar(r=df_test['frequency'], theta=df_test['direction'],fill='toself',fillcolor='green'))
fig.show(config= {'displaylogo': False})
Add the same color to the line so that the hover color will also be the same. I then disabled the grid on the graph to match the image presented.
import pandas as pd
import plotly.graph_objects as go
df = px.data.wind()
df_test = df[df["strength"]=='0-1']
fig = go.Figure()
fig.add_trace(go.Scatterpolar(r=df_test['frequency'],
theta=df_test['direction'],
fill='toself',
fillcolor='green',
line_color='green',
))
fig.update_layout(polar=dict(radialaxis=dict(visible=False)))
fig.show(config= {'displaylogo': False})
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)
I was trying to create some heatmap using plotly3.10 and I encountered one problem that the
column names are not displayed full in ylabel.
import pandas as pd
import plotly.figure_factory as ff
from plotly.offline import plot, iplot, init_notebook_mode
df = pd.util.testing.makeDataFrame()
df.columns = ['this_is_long_column_name','another_column_name','yet_another_column_name','price']
df_corr = df.corr()
z = df_corr.values
fig = ff.create_annotated_heatmap(z,showscale=True,
x=df_corr.columns.values.tolist(),
y=df_corr.columns.values.tolist()
)
iplot(fig)
I got this image:
Question
How to show the full column name in ylabels?
How to show xlabel on both top and bottom with larger fontsizes?
How to show only 2 significant numbers, like df.round(2) only in plot?
Have you tried manually specifying the margins? E.g.:
import plotly.graph_objs as go
layout = go.Layout(
margin=dict(l=80, r=80, t=100, b=80)
)
This might work for you:
import numpy as np
import pandas as pd
import plotly
import plotly.offline as py
import plotly.graph_objs as go
import plotly.figure_factory as ff
from plotly.offline import plot, iplot, init_notebook_mode
init_notebook_mode(connected=False)
df = pd.util.testing.makeDataFrame()
df.columns = ['this_is_long_column_name','another_column_name','yet_another_column_name','price']
df_corr = df.corr()
z = df_corr.round(2).values
fig = ff.create_annotated_heatmap(z,showscale=True,
x=df_corr.columns.values.tolist(),
y=df_corr.columns.values.tolist()
)
layout = go.Layout(margin=dict(l=200, r=50, t=100, b=50))
fig.layout.update(layout)
iplot(fig)
Gives:
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 have been trying to plot a table and a scatter plot side by side using plotly but to no avail for a long period. How can we do that?
How to plot table and scatter plots in plotly3.6?
I have tried this so far:
import numpy as np
import pandas as pd
import seaborn as sns
sns.set(color_codes=True)
import matplotlib.pyplot as plt
%matplotlib inline
import plotly
import plotly.offline as py
import plotly.plotly as pyp
import plotly.graph_objs as go
import plotly.figure_factory as ff
import plotly.tools as tls
from plotly.offline import plot, iplot, init_notebook_mode
init_notebook_mode(connected=False)
# subplots table
fig = tls.make_subplots(rows=1, cols=2, shared_xaxes=True)
trace1 = go.Scatter(x=[1,2,3],y=[1,2,3])
df = pd.DataFrame({'name':list('ABC'),
'salary': [1000,2000,3000]})
table = ff.create_table(df)
fig.append_trace(table, 1, 2)
py.iplot(fig)
But this does not work.
However,
iplot(table) works.
How to fix the problem?
You are close but forgot the specs element. Also, you must specify positioning in .add_trace() with row=# and col=# Hope this helps.
from plotly.subplots import make_subplots
columns =['one','two']
cellValues =['first piece of data', 'second piece of data']
figure = make_subplots(
rows = 1, cols =2,
specs=[[{"type":"table"},{"type" : "scatter"}]]
)
figure.add_trace(go.Table(header=dict(
values = columns
#other elements here
),cells=dict(
values = cellValues
), row=1, col=1
)
figure.add_trace(go.Scatter(x=[1,2,3],y=[1,2,3]),row=1,col=2)
)
ff.create_table() creates a full Figure not a trace, so you can't append it to a pre-existing Figure. You'll have to go the other way and add the Scatter to the output of ff.create_table().