spyder plotly : issue with plotting barplots - python

please notice barplot('Age',False) runs without any error but fails to plot graph in chromeI'm using spyder to analyse employee attrition of ibm hr employee dataset from kaggle. For plotting graphs I'm using plotly library but I can't plot barplots using this library whereas I can successfully plot scatter plots and pie charts.
Attaching sample code of my work
spyder was successful to print plot_distribution('Age', False) this graph in chrome but can't plot barplot('Age', False)
def barplot(var_select, x_no_numeric) :
tmp1 = data[(data['Attrition'] != 0)]
tmp2 = data[(data['Attrition'] == 0)]
tmp3 = pd.DataFrame(pd.crosstab(data[var_select], data['Attrition']), )
tmp3['Attr%'] = tmp3[1] / (tmp3[1] + tmp3[0]) * 100
if x_no_numeric == True:
tmp3 = tmp3.sort_values(1, ascending=False)
color = ['skyblue', 'gold']
trace1 = go.Bar(
x = tmp1[var_select].value_counts().keys().tolist(),
y = tmp2[var_select].value_counts().values.tolist(),
name = 'yes_attrition', opacity = 0.8,
marker = dict(color = 'gold', line=dict(color='#000000',width=1)))
trace2 = go.Bar(
x = tmp1[var_select].value_counts().keys().tolist(),
y = tmp2[var_select].value_counts().values.tolist(),
name = 'no_attrition', opacity = 0.8,
marker = dict(color='skyblue', line=dict(color='#000000',width=1)))
trace3 = go.Scatter(
x = tmp3.index,
y = tmp3['Attr%'],
yaxis = 'y2',
name = '%Attrition', opacity= 0.5,
marker= dict(color='black',line=dict(color='#000000',width=0.5)))
layout = dict(title= str(var_select),
xaxis = dict(),
yaxis = dict(title='count'),
yaxis2 = dict(range= [-0,75],
overlaying = 'y',
anchor = 'x',
side = 'right',
Zeroline = False,
showgrid = False,
title = '%Attrition'))
fig = go.Figure(data=[trace1, trace2, trace3], layout= layout)
py.plot(fig)
plot_distribution('Age', False)
barplot('Age', False)

Related

Plotly - Table not displaying properly until column moved / rerendered

I am using Plotly to display data in a simple table. I've populated in some dummy data below.
import plotly.graph_objects as go
data = [go.Table(
columnorder = [1,2],
header = dict(
values = ['<b>'+'TENOR'+'</b>'] + list(['a','b','c','d','e']),
line_color = 'darkslategray',
fill_color = 'royalblue',
align = 'center',
font = dict(color = 'white', size = 12),
height = 20
),
cells = dict(
values = [['row1','row2','row3'],[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5]],
line_color = 'darkslategray',
fill = dict(color=['paleturquoise', 'white']),
align = ['right', 'center'],
font_size = 12,
height = 20)
)]
fig = go.Figure(data = data)
fig.show()
When I run this (Jupyter, but same result happens in a couple other IDEs too) it initially displays as:
Then, as soon as I move a column / manipulate it in any way, it then redisplays (properly, except the column that I've moved) as:
Any idea why it's not rendering correctly in the first place? I basically took this right from one of their table examples...
Comment out your line columnorder = [1,2,3,4,5],.
import plotly.graph_objects as go
data = [go.Table(
#columnorder = [1,2],
header = dict(
values = ['<b>'+'TENOR'+'</b>'] + list(['a','b','c','d','e']),
line_color = 'darkslategray',
fill_color = 'royalblue',
align = 'center',
font = dict(color = 'white', size = 12),
height = 20
),
cells = dict(
values = [['row1','row2','row3'],[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5]],
line_color = 'darkslategray',
fill = dict(color=['paleturquoise', 'white']),
align = ['right', 'center'],
font_size = 12,
height = 20)
)]
fig = go.Figure(data = data)
fig.show()
Specifying that is saying you only want to show those listed columns.
Alternatively, change that line to columnorder = [0,1,2,3,4,5],.

How to change color of bar graph by mouse click event in matplotlib?

I want to create a bar graph in which, after a mouse click event, the colors of the bar change according to the color bar. The selected value is displayed with a horizontal threshold line and the bars are supposed to change color depending on the probability of the selected value being above or below the mean of the sample.
I can't seem to wrap my head around how to connect the mouse click event and the color bar values to the bar graph values and produce the desired result.(color bar depicts probability)
I am new to this and any feedback would be appreciated.
df = df1.T
mean = list(df.mean().values)
std = list(df.std().values)
x = range(1,len(mean)+1)
sqrt = len(df.iloc[:,0:1])**0.5
CI = []
for i in range(len(std)):
CI.append((1.96*std[i])/sqrt)
years = [1992,1993,1994,1995]
prob = np.arange(0,1.1,0.1)
start_line_value = 5000
norm = matplotlib.colors.Normalize(30e3, 60e3)
plt.xticks([i for i in range(1,len(years)+1)],years)
color_map = plt.cm.get_cmap('seismic_r')
colors = color_map(mean)
CS = plt.contourf([x,mean],prob,cmap=color_map)
cbar = plt.colorbar(CS, cmap=color_map)
cbar.set_label('Probability', rotation=0,labelpad=25)
bars = plt.bar(x,mean,width = 1,color = plt.cm.seismic_r(norm(mean)),yerr = CI ,align = 'center',capsize = 10,alpha = 0.99)
ax = plt.axes()
class AfterClicking(object):
def __init__(self,ax):
self.fig = ax.figure
self.ax = ax
self.start_line = ax.axhline(y = start_line_value,linewidth = 1,color = 'black')
trans = transforms.blended_transform_factory(ax.get_yticklabels()[0].get_transform(), ax.transData)
self.value = ax.text(0,start_line_value, "{:.0f}".format(start_line_value), color="blue", transform=trans,ha="right", va="center")
self.fig.canvas.mpl_connect('button_press_event', self.draw_line_on_click)
ax.set_title('Marker at {} on Y axis'.format(start_line_value))
def draw_line_on_click(self,event):
self.start_line.remove()
self.value.remove()
self.start_line = plt.axhline(y = event.ydata ,linewidth = 1,color = 'black')
trans = transforms.blended_transform_factory(ax.get_yticklabels()[0].get_transform(), ax.transData)
self.value = ax.text(0,event.ydata, "{:.0f}".format(event.ydata), color="blue", transform=trans,ha="right", va="center")
ax.set_title('Marker at {} on Y axis'.format(event.ydata))
answer = AfterClicking(ax)

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:))

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')

Categories