Set coordinate origin plotly python - python

This is my code:
trace1 = go.Scatterpolar(
r = r,
theta = theta,
mode='markers',
marker=dict(
size=12,
color= iris.target,
opacity=1
)
)
dc_1 = go.Scatterpolar( r = [0,V_r[0][0]],
theta = [0,V_r[0][1]],
marker = dict( size = 1,
color = "rgb(84,48,5)"),
line = dict( color = "red",
width = 6),
name = "Var1"
)
dc_2 = go.Scatterpolar( r = [0,V_r[1][0]],
theta = [0,V_r[1][1]],
marker = dict( size = 1,
color = "rgb(84,48,5)"),
line = dict( color = "green",
width = 6),
name = "Var2"
)
dc_3 = go.Scatterpolar( r = [0,V_r[2][0]],
theta = [0,V_r[2][1]],
marker = dict( size = 1,
color = "rgb(84,48,5)"),
line = dict( color = "blue",
width = 6),
name = "Var3"
)
dc_4 = go.Scatterpolar( r = [0,V_r[3][0]],
theta = [0,V_r[3][1]],
marker = dict( size = 1,
color = "rgb(84,48,5)"),
line = dict( color = "yellow",
width = 6),
name = "Var4"
)
data = [dc_1,dc_2,dc_3,dc_4, trace1]
layout = go.Layout(
xaxis=dict(
title='PC1',
rangemode='tozero',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#080707'
)
)
)
fig = go.Figure(data=data, layout=layout)
plot(fig, filename='Scatter polar, PCA.')
And this is the graph resultant:
enter image description here
As you can see, currently the lines of the variables start from "0", but the origin of coordinates is in r = - 5 (since that is the first value it receives) how can I set it to r = 0?

r has to be represented in absolute value, since the address gives you the value of theta. r cannot be < 0:
r = abs(np.squeeze(np.asarray(P[:,0])))
theta = (np.squeeze(np.asarray(P[:,1])))*(180/math.pi)

Related

How to keep the same color order for multiple pie plots?

I am going through multiple columns and creating plotly pie charts, to check the distribution of these variables between two types of customer: power user and not power user. However the color order changes every time.
In one plot, Yes will be orange and No will , in the next plot, Yes will be blue and No will be orange.
You can see an example here, if you check the legend between the two charts:
Here is the code I am using. After some googling I tried adding sort=False to go.pie but it didn't change anything:
def plot_pie(column) :
trace1 = go.Pie(values = churn[column].value_counts().values.tolist(),
labels = churn[column].value_counts().keys().tolist(),
hoverinfo = "label+percent+name",
domain = dict(x = [0,.48]),
name = f"{target}",
marker = dict(line = dict(width = 2,
color = "rgb(243,243,243)")
),
hole = .6,
sort=False
)
trace2 = go.Pie(values = not_churn[column].value_counts().values.tolist(),
labels = not_churn[column].value_counts().keys().tolist(),
hoverinfo = "label+percent+name",
marker = dict(line = dict(width = 2,
color = "rgb(243,243,243)")
),
domain = dict(x = [.52,1]),
hole = .6,
name = f"Not {target}" ,
sort=False
)
layout = go.Layout(dict(title = column + f" distribution in {target} ",
plot_bgcolor = "rgb(243,243,243)",
paper_bgcolor = "rgb(243,243,243)",
annotations = [dict(text = f"{target}",
font = dict(size = 13),
showarrow = False,
x = .15, y = .5),
dict(text = f"not {target}",
font = dict(size = 13),
showarrow = False,
x = .88,y = .5
)
]
)
)
data = [trace1,trace2]
fig = go.Figure(data = data,layout = layout)
py.iplot(fig)
Any advice?

How create a slider for a custom chorpleth map using plotly in python?

I'm create a choropleth map using plotly, geojso and matplotlib. Now i want to add a slider.
i'm using python3 and i can't find a answer for this in the internet.all of them used plotly's default choropleth map.
def get_scatter_colors(sm, df):
grey = 'rgba(128,128,128,1)'
return ['rgba' + str(sm.to_rgba(m, bytes = True, alpha = 1)) if not np.isnan(m) else grey for m in df]
for age in columns[3:24]:
age_data = df[age]
age_data.name = 'province'
# print(age_data.head())
df_tmp = age_data.copy()
df_tmp.index = df_tmp.index.map(match_dict)
df_tmp = df_tmp[~df_tmp.index.duplicated(keep=False)]
df_reindexed = df_tmp.reindex(index = provinces_names)
colormap = 'Blues'
cmin = df_reindexed.min()
cmax = df_reindexed.max()
sm = scalarmappable(colormap, cmin, cmax)
scatter_colors = get_scatter_colors(sm, df_reindexed)
colorscale = get_colorscale(sm, df_reindexed, cmin, cmax)
hover_text = get_hover_text(df_reindexed)
scatter_color_list.append(scatter_colors)
tickformat = ""
data = dict(type='scattermapbox',
lat=lats,
lon=lons,
mode='markers',
text=hover_text,
marker=dict(size=10,
color=scatter_colors,
showscale = True,
cmin = df_reindexed.min(),
cmax = df_reindexed.max(),
colorscale = colorscale,
colorbar = dict(tickformat = tickformat)
),
showlegend=False,
hoverinfo='text'
)
data_slider.append(data)
layers=([dict(sourcetype = 'geojson',
source =sources[k],
below="",
type = 'line', # the borders
line = dict(width = 1),
color = 'black',
) for k in range(n_provinces)
] +
# fill_list
[dict(sourcetype = 'geojson',
source =sources[k],
below="water",
type = 'fill',
color = scatter_colors[k],
opacity=0.8,
) for k in range(n_provinces)
]
)
steps = []
for i in range(len(data_slider)):
step = dict(method='restyle',
args=['visible', [False] * len(data_slider)],
label='Age {}' .format(i))
step['args'][1][i] = True
steps.append(step)
sliders = [dict(active=0, steps=steps)]
layout = dict(title="2016 POPULATION",
autosize=False,
width=700,
height=800,
hovermode='closest',
# hoverdistance = 30,
mapbox=dict(accesstoken=MAPBOX_APIKEY,
layers=layers,
bearing=0,
center=dict(
lat=35.715298,
lon=51.404343),
pitch=0,
zoom=4.9,
style = 'light'),
sliders=sliders,
)
when i test this code with data, i want changing color of the map with slides , but the color of map is fixed by the lastest slide color.
Edit step part like this:
visibility = []
for i in range(len(data_slider)):
list = [False] * len(data_slider)
list[i] = True
visibility.append(list)
steps = []
for i in range(len(data_slider)):
step = dict(method='update',
args=[{'visible': visibility[i]},
{'mapbox.layers': layers[i]}],
label='Age {}' .format(i),)
steps.append(step)
And add function for layers:
def get_data_layout(df):
layers = []
for i in range(len(data_slider)):
scatter_colors = df[i]['marker']['color']
layer=([dict(sourcetype = 'geojson',
source =sources[k],
below="",
type = 'line',
line = dict(width = 1),
color = 'black',
) for k in range(n_provinces)
] +
[dict(sourcetype = 'geojson',
source =sources[k],
below="water",
type = 'fill',
color = scatter_colors[k],
opacity=0.8,
) for k in range(n_provinces)]
)
layers.append(layer)
return layers
and change layout to this:
layout = dict(title="IRAN 2016 POPULATION",
autosize=False,
width=700,
height=800,
hovermode='closest',
mapbox=dict(accesstoken=MAPBOX_APIKEY,
bearing=0,
center=dict(
lat=35.715298,
lon=51.404343),
pitch=0,
zoom=4.9,
style = 'dark'),
sliders=sliders,
)
other parts are same:))

I want to display the data as percentages on the x-axis in plotly (python)

I want to display the data on x-axis as a percentage.
I tried different methods including the #tickformat method, but wasn't able to successfully get the required output.
trace4 = go.Bar(
y=[''],
x=[5731],
name='Other(Used/Available)',
text = ("5731"),
textposition = 'inside',
orientation = 'h',
marker = dict(
color = 'rgba(210, 105, 30, 0.6)',
line = dict(
color = 'rgba(210, 105, 30, 1.0)',
width = 3)
)
)
data = [trace1, trace2, trace3, trace4]
layout = go.Layout(
autosize=False,
width=800,
height=275,
barmode='stack',
xaxis=dict(tickformat="%")
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='marker-h-bar')
Alright, I figured this out myself. I calculated the total, and then calculated the percentage that way. After that, just used xaxis=dict(ticksuffix="%") to assign the "%" symbol.
l = []
for i in rows2:
l.append(list(i))
schema = [i[0] for i in l]
nos = [i[1] for i in l]
nos = [int(x) for x in nos]
i = 0
t = 0
while i < len(nos):
t += nos[i]
i += 1
P = [float(x/t*100) for x in nos]
trace1 = go.Bar(
y=[''],
x=[P[0]],
name = (f"{schema[0]}"),
text = (f"{nos[0]}"),
textposition = 'auto',
orientation = 'h',
marker = dict(
color = 'rgba(103, 240, 160, 0.75)',
line = dict(
color = '#43BD78',
width = 3)
)
)
trace2 = go.Bar(
y=[''],
x=[P[1]],
name=(f"{schema[1]}"),
text = (f"{nos[1]}"),
textposition = 'auto',
orientation = 'h',
marker = dict(
color = 'rgba(255, 172, 188, 0.75)',
line = dict(
color = '#E67188',
width = 3)
)
)
trace3 = go.Bar(
y=[''],
x=[P[2]],
name=(f"{schema[2]}"),
text = (f"{nos[2]}"),
textposition = 'auto',
orientation = 'h',
marker = dict(
color = 'rgba(177, 193, 216, 1.0)',
line = dict(
color = '#82A1D0',
width = 3)
)
)
trace4 = go.Bar(
y=[''],
x=[P[3]],
name=(f"{schema[3]}"),
text = (f"{nos[3]}"),
textposition = 'auto',
orientation = 'h',
marker = dict(
color = 'rgba(250, 184, 126, 0.75)',
line = dict(
color = '#DF8E47',
width = 3)
)
)
trace5 = go.Bar(
y=[''],
x=[P[4]],
name=(f"{schema[4]}"),
text = (f"{nos[4]}"),
textposition = 'inside',
orientation = 'h',
marker = dict(
color = '#C8A8CD',
line = dict(
color = '#9C7DA1',
width = 3)
)
)
data = [trace1, trace2, trace3, trace4, trace5]
layout = go.Layout(
autosize=False,
width=700,
height=110,
barmode='stack',
xaxis=dict(ticksuffix="%"),
legend=dict(
orientation="h",
x=0.05,
y=-0.27,
font=dict(
size=10.5
)
),
margin=go.layout.Margin(
l=60,
r=60,
b=0,
t=0,
pad=0
)
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='marker-h-bar')

3D biplot in Plotly in Python

I want to plot something like a biplot in python Plotly ,but using 3 principal components so as to make a 3d plot.
How do I go about plotting the direction vectors(the red lines) of principal components in plotly python?
There is exactly the same question , but for R, here. I am not able to translate the code perfectly.
With much help from here
from plotly.offline import plot
import plotly.graph_objs as go
pca = PCA(n_components=3).fit(iris.data)
X_reduced = pca.transform(iris.data)
trace1 = go.Scatter3d(
x=X_reduced[:,0],
y = X_reduced[:,1],
z = X_reduced[:,2],
mode='markers',
marker=dict(
size=12,
color= target,
opacity=1
)
)
dc_1 = go.Scatter3d( x = [0,pca.components_.T[0][0]],
y = [0,pca.components_.T[0][1]],
z = [0,pca.components_.T[0][2]],
marker = dict( size = 1,
color = "rgb(84,48,5)"),
line = dict( color = "red",
width = 6),
name = "Var1"
)
dc_2 = go.Scatter3d( x = [0,pca.components_.T[1][0]],
y = [0,pca.components_.T[1][1]],
z = [0,pca.components_.T[1][2]],
marker = dict( size = 1,
color = "rgb(84,48,5)"),
line = dict( color = "green",
width = 6),
name = "Var2"
)
dc_3 = go.Scatter3d( x = [0,pca.components_.T[2][0]],
y = [0,pca.components_.T[2][1]],
z = [0,pca.components_.T[2][2]],
marker = dict( size = 1,
color = "rgb(84,48,5)"),
line = dict( color = "blue",
width = 6),
name = "Var3"
)
dc_4 = go.Scatter3d( x = [0,pca.components_.T[3][0]],
y = [0,pca.components_.T[3][1]],
z = [0,pca.components_.T[3][2]],
marker = dict( size = 1,
color = "rgb(84,48,5)"),
line = dict( color = "yellow",
width = 6),
name = "Var4"
)
data = [trace1,dc_1,dc_2,dc_3,dc_4]
layout = go.Layout(
xaxis=dict(
title='PC1',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
)
)
fig = go.Figure(data=data, layout=layout)
plot(fig, filename='3d-scatter-tupac-with-mac')

How to create a subplot with shared x-axis and range slider in Python Plotly

I'm trying to create a specific plot with Python and Plotly. I was wondering if it's possible to create a plot with 3 subplots arranged vertically (https://plot.ly/python/subplots/) that have a shared x-axis, along with a range slider that controls the x-axis (https://plot.ly/python/range-slider/)?
As of Jan 2017 you cannot do this, see here: https://github.com/plotly/plotly.js/issues/1250
Today, Jan 24th 2017, I have managed to create stacked plots that share one x-axis, in combination with a range slider. However, the problem is that the range of the y-axis is automatically set. I cannot control it. This is a plroblem for me. My code is:
trace_1 = go.Scatter(
x=time_station1,
y=turb_station1,
mode = 'lines+markers',
name = 'Turbidity',
connectgaps = False,
marker = dict(
size = 5,
color = 'rgb(64, 97, 139)',
line = dict(
width = 1,
color = 'rgb(64, 97, 139)'
)
)
)
trace_2 = go.Scatter(
x=time_station1,
y=battery_station1,
yaxis='y2',
mode = 'lines+markers',
name = 'Battery',
connectgaps = False,
marker = dict(
size = 5,
color = 'rgb(117, 15, 7)',
line = dict(
width = 1,
color = 'rgb(117, 15, 7)'
)
)
)
trace_3 = go.Scatter(
x=time_station1,
y=cond_station1,
yaxis='y3',
mode = 'lines+markers',
name = 'Conductivity',
connectgaps = False,
marker = dict(
size = 5,
color = 'rgb(130, 0, 132)',
line = dict(
width = 1,
color = 'rgb(130, 0, 132)'
)
)
)
trace_4 = go.Scatter(
x=time_station1,
y=depth_station1,
yaxis='y4',
mode = 'lines+markers',
name = 'Depth',
connectgaps = False,
marker = dict(
size = 5,
color = 'rgb(204, 100, 0)',
line = dict(
width = 1,
color = 'rgb(204, 100, 0)'
)
)
)
trace_5 = go.Scatter(
x=time_station1,
y=temp_station1,
yaxis='y5',
mode = 'lines+markers',
name = 'Temperature',
connectgaps = False,
marker = dict(
size = 5,
color = 'rgb(255, 255, 0)',
line = dict(
width = 1,
color = 'rgb(255, 255, 0)'
)
)
)
layout = go.Layout(
title='Station ABC',
xaxis = dict(
rangeselector=dict(
buttons = list([
dict(count=1,
label='1min',
step='minute',
stepmode='backward'),
dict(count=24,
label='24h',
step='hour',
stepmode='backward'),
])
),
rangeslider=dict(),
type='date',
title='Date and Time'
),
yaxis=dict(
domain=[0,0.15]),
yaxis2=dict(
domain=[0.2,0.35]),
yaxis3=dict(
domain=[0.4,0.55]),
yaxis4=dict(
domain=[0.4,0.75]),
yaxis5=dict(
domain=[0.8,1]),
)
data = [trace_1, trace_2, trace_3, trace_4, trace_5]
plot_url = py.plot(fig, filename='offline plot.html')

Categories