i am trying to create subplots for the following plots:
the code i used for the the plots are :
radius_mean1 = df[df['diagnosis']==1]['radius_mean']
radius_mean0 = df[df['diagnosis']==0]['radius_mean']
trace_rm1 = go.Histogram(x = radius_mean1, opacity = 0.75, name = 'malignant')
trace_rm2 = go.Histogram(x = radius_mean0, opacity = 0.75, name = 'benign')
data2 = [trace_rm1,trace_rm2]
layout2 = go.Layout(barmode = 'overlay', title = 'radius mean')
fig2 = go.Figure(data=data2, layout=layout2)
py.iplot(fig2)
and similar for the other plot
now i use the following code i found to create subplots:
fig.append_trace(fig1['data'][1], 1, 1)
fig.append_trace(fig2['data'][0], 2, 1)
py.iplot(fig)
and i get this :
how do i add both benign and malignant results to the subplots ?
i cant seem to edit the [0] or [ 1] to show both ie have the first 2 plots exactly into my subplot showing both malignant and benign and not just one or the other.
The data here is not complete, so I using some demo data to present.
import numpy as np
import plotly.graph_objs as go
import plotly
a = np.random.normal(0,1,100)
b = np.random.normal(-2,5,100)
c = np.random.normal(0,1,100)
d = np.random.normal(-2,5,100)
fig = plotly.tools.make_subplots(rows=2,cols=1)
trace_rm1 = go.Histogram(x = a, opacity = 0.75, name = 'malignant')
trace_rm2 = go.Histogram(x = b, opacity = 0.75, name = 'benign')
fig.append_trace(go.Histogram(x = a, opacity = 0.75, name = 'benign'),1,1)
fig.append_trace(go.Histogram(x = b, opacity = 0.75, name = 'malignant'),1,1)
fig.append_trace(go.Histogram(x = c, opacity = 0.75, name = 'benign'),2,1)
fig.append_trace(go.Histogram(x = d, opacity = 0.75, name = 'malignant'),2,1)
fig.layout.update(go.Layout(barmode = 'overlay',))
plotly.offline.plot(fig)
Related
I want to keep the labels when you hover, but hide the labels from just appearing over the Sankey as text.
Here is my code:
labels = df_mapping['Name'].to_numpy().tolist() + labels
count_dict = {}
source = []
target = []
value = df_subset['Stuff'].to_numpy().tolist()
index = 0
for x in unique_broad:
count_dict[x] = len(df_mapping.loc[df_mapping['Stuff'] == x])
for key in count_dict:
for i in range(count_dict[key]):
source.append(index)
index += 1
for key in count_dict:
for i in range(count_dict[key]):
target.append(index)
index += 1
number_of_colors = len(source)
color_link = ["#"+''.join([random.choice('0123456789ABCDEF') for j in range(6)])
for i in range(number_of_colors)]
link = dict(source=source, target=target, value=value, color=color_link)
node = dict(label=labels, pad=35, thickness=10)
data = go.Sankey(link=link, node=node)
fig = go.Figure(data)
fig.update_layout(
hovermode = 'x',
title="Sankey for Stuff",
font=dict(size=8, color='white'),
paper_bgcolor='#51504f'
)
return fig
You can make the labels invisible by setting the color of the labels to rgba(0,0,0,0). This ensures that the label will remain in the hovertemplate, but not show up on the nodes.
To do this you can pass textfont=dict(color="rgba(0,0,0,0)", size=1) to go.Sankey such as in the example you used from the Plotly sankey diagram documentation:
import plotly.graph_objects as go
import urllib.request, json
url = 'https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json'
response = urllib.request.urlopen(url)
data = json.loads(response.read())
# override gray link colors with 'source' colors
opacity = 0.4
# change 'magenta' to its 'rgba' value to add opacity
data['data'][0]['node']['color'] = ['rgba(255,0,255, 0.8)' if color == "magenta" else color for color in data['data'][0]['node']['color']]
data['data'][0]['link']['color'] = [data['data'][0]['node']['color'][src].replace("0.8", str(opacity))
for src in data['data'][0]['link']['source']]
fig = go.Figure(data=[go.Sankey(
textfont=dict(color="rgba(0,0,0,0)", size=1),
valueformat = ".0f",
valuesuffix = "TWh",
# Define nodes
node = dict(
pad = 15,
thickness = 15,
line = dict(color = "black", width = 0.5),
label = data['data'][0]['node']['label'],
color = data['data'][0]['node']['color']
),
# Add links
link = dict(
source = data['data'][0]['link']['source'],
target = data['data'][0]['link']['target'],
value = data['data'][0]['link']['value'],
label = data['data'][0]['link']['label'],
color = data['data'][0]['link']['color']
))])
fig.update_layout(title_text="Energy forecast for 2050<br>Source: Department of Energy & Climate Change, Tom Counsell via <a href='https://bost.ocks.org/mike/sankey/'>Mike Bostock</a>",
font_size=10)
fig.show()
You get the following:
I have the following data lists:
date = ['2019-01-01', '2019-01-18', '2019-02-03']
value1 = [6798.0, 436.0, 348.0]
value2 = [500.0, 455.0, 348.0]
From these lists I generate a line graph using the plotly library, this way:
trace_1 = go.Scatter(x = date,
y = value1,
mode = 'markers+lines',
marker_color='rgb(152, 0, .8)',
name = '1')
trace_2 = go.Scatter(x = date,
y = value2,
mode = 'markers+lines',
marker_color='rgb(0, 0, .8)',
name = '2')
data_total = [trace_1, trace_2]
fig = go.Figure(data=data_total, layout=layout)
py.iplot(fig)
The graphic is working perfectly and the lines are in different colors. However, I would like the line colors, instead of manual as I did, to be automatically generated randomly.
Following this tutorial: https://www.kite.com/python/answers/how-to-generate-random-colors-in-matplotlib-in-python
I tried to do:
colors = np.random.rand(1,3)
trace_1 = go.Scatter(x = date,
y = value1,
mode = 'markers+lines',
marker_color = colors,
name = '1')
trace_2 = go.Scatter(x = date,
y = value2,
mode = 'markers+lines',
marker_color = colors,
name = '2')
data_total = [trace_1, trace_2]
fig = go.Figure(data=data_total, layout=layout)
py.iplot(fig)
But this code doesn't work.
You should only need to comment out marker_color. Many plotting libraries (plotly included) tend to automatically assign colors.
Which line of this code:
# Take credit amount values into a list
young = df['Credit_amount'].loc[df['Age_Group'] == 'Young'].values.tolist()
young_adults = df['Credit_amount'].loc[df['Age_Group'] == 'Young Adults'].values.tolist()
senior = df['Credit_amount'].loc[df['Age_Group'] == 'Senior'].values.tolist()
elder_credit = df['Credit_amount'].loc[df['Age_Group'] == 'Elder'].values.tolist()
# Create the box plots by age category
young_credit = go.Box(
y = young,
name = "Young",
jitter = 0.3,
pointpos = -1.8,
boxpoints = 'all',
marker = dict(
color = 'rgb(150, 198, 109)'),
line = dict(
color = 'rgb(111, 200, 37)')
)
young_adults_credit = go.Box(
y = young_adults,
name = "Young Adults",
jitter = 0.3,
pointpos = -1.8,
boxpoints = 'all',
marker = dict(
color = 'rgb(124, 236, 212)'),
line = dict(
color = 'rgb(38, 214, 177)')
)
senior_credit = go.Box(
y = senior,
name = "Seniors",
jitter = 0.3,
pointpos = -1.8,
boxpoints = 'all',
marker = dict(
color = 'rgb(241, 93, 93)'),
line = dict(
color = 'rgb(225, 44, 44)')
)
elder_credit = go.Box(
y = elder_credit,
name = "Elders",
jitter = 0.3,
pointpos = -1.8,
boxpoints = 'all',
marker = dict(
color = 'rgb(180, 121, 72)'),
line = dict(
color = 'rgb(115, 77, 46)')
)
data = [young_credit, young_adults_credit, senior_credit, elder_credit]
layout = dict(
title="Credit Amount by Age Group Segment",
xaxis = dict(title="Age Group"),
yaxis= dict(title="Credit Amount")
)
fig = dict(data=data, layout=layout)
iplot(fig, filename="Box Plot")
concerns the fragments marked in the picture below, I would like to remove those fragments from the chart and which lines of code I have to remove to achieve this goal.
I will be really thankfull for all clear answers because I can not find line of code to remove this fragments of plot.
Thank you so much!
If you Want to totally remove the points, you should remove parameters in each go.Box:
jitter = 0.3,
pointpos = -1.8,
boxpoints = 'all'
From plot.ly/python/box-plots/: With the points argument, display underlying data points with either all points (all), outliers only (outliers, default), or none of them (False).
Plot 1: boxpoints = False
Plot 2: boxpoints = 'all'
I got the same issue and can still not find the fix. The github issue is still open which Ahmed mentioned (https://github.com/plotly/plotly.js/issues/277).
Although, you can use a visible work around. It does not fix the problem! But for the vision it is fixed.
You van marker=dict(opacity=0) which makes the points invisible. When you hover over them, they are still there.
With Bokeh, how do I get a handle to the Renderer (or GlyphRenderer) for an Annotation? Is this possible?
I would like to be able to toggle a Band (which is an Annotation) on and off with an interactive legend, so I need to be able to pass a list of Renderers to the LegendItem constructor.
This code:
maxline = fig.line(x='Date', y=stn_max, line_width=0.5, legend=stn_max, name="{}_line".format(stn_max), color=stn_color, alpha=0.75, source=source)
minline = fig.line(x='Date', y=stn_min, line_width=0.5, legend=stn_min, name="{}_line".format(stn_min), color=stn_color, alpha=0.75, source=source)
band = bkm.Band(base='Date', lower=stn_min, upper=stn_max, fill_alpha=0.50, line_width=0.5, fill_color=stn_color, source=source)
bkm.LegendItem(label=stn, renderers=[maxline, minline, band])
Produces this error
...
ValueError: expected an element of List(Instance(GlyphRenderer)), got seq with invalid items [Band(id='1091', ...)]
For LegendItem only instances of GlyphRenderer can be passed to its renderers attribute and Band is not based on GlyphRenderer so it gives error. In the code below the Band visibility is being toggled by means of a callback:
from bokeh.plotting import figure, show
from bokeh.models import Band, ColumnDataSource, Legend, LegendItem, CustomJS
import pandas as pd
import numpy as np
x = np.random.random(2500) * 140 - 20
y = np.random.normal(size = 2500) * 2 + 5
df = pd.DataFrame(data = dict(x = x, y = y)).sort_values(by = "x")
sem = lambda x: x.std() / np.sqrt(x.size)
df2 = df.y.rolling(window = 100).agg({"y_mean": np.mean, "y_std": np.std, "y_sem": sem})
df2 = df2.fillna(method = 'bfill')
df = pd.concat([df, df2], axis = 1)
df['lower'] = df.y_mean - df.y_std
df['upper'] = df.y_mean + df.y_std
source = ColumnDataSource(df.reset_index())
p = figure(tools = "pan,wheel_zoom,box_zoom,reset,save")
scatter = p.scatter(x = 'x', y = 'y', line_color = None, fill_alpha = 0.3, size = 5, source = source)
band = Band(base = 'x', lower = 'lower', upper = 'upper', source = source)
p.add_layout(band)
p.title.text = "Rolling Standard Deviation"
p.xaxis.axis_label = 'X'
p.yaxis.axis_label = 'Y'
callback = CustomJS(args = dict(band = band), code = """
if (band.visible == false)
band.visible = true;
else
band.visible = false; """)
legend = Legend(items = [ LegendItem(label = "x", renderers = [scatter, band.source.selection_policy]) ])
legend.click_policy = 'hide'
scatter.js_on_change('visible', callback)
p.add_layout(legend)
show(p)
Result:
Why plotly package of python can not display figure in RMarkdown but matplotlib can? For example:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
```
```{r}
library(plotly)
subplot(
plot_ly(mpg, x = ~cty, y = ~hwy, name = 'default'),
plot_ly(mpg, x = ~cty, y = ~hwy) %>%
add_markers(alpha = 0.2, name = 'alpha'),
plot_ly(mpg, x = ~cty, y = ~hwy) %>%
add_markers(symbols = I(1), name = 'hollow')
)
```
```{python}
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np
plotly.tools.set_credentials_file(username='xxx', api_key='xxx')
N = 500
trace0 = go.Scatter(x = np.random.randn(N), y = np.random.randn(N) + 2, name = "Above", mode = "markers",
marker = dict(size = 10, color = "rgba(152, 0, 0, .8)", line = dict(width = 2, color = "rgb(0,0,0)")))
trace1 = go.Scatter(x = np.random.randn(N), y = np.random.randn(N) - 2, name = "below", mode = "markers",
marker = dict(size = 10, color = "rgba(255, 182, 193, .9)", line = dict(width = 2, color = "rgb(0,0,0)")))
data = [trace0, trace1]
layout = dict(title = "Styled Scatter", yaxis = dict(zeroline = False), xaxis = dict(zeroline=False))
fig = dict(data = data, layout = layout)
py.iplot(fig, filename = "styled-scatter")
```
The R code can work well, but the python code can not dispay the figure, what is wrong with the code?
Here is what I did:
used plotly offline:
replace import plotly.plotly as py by import plotly.offline as py
no need to set username and api key in offline mode.
used py.plot(fig, filename = "styled-scatter.html", auto_open=False):
py.iplot() is for Jupyter notebooks (it embeds the plot directly into the Notebook)
auto_open = False argument is to avoid that the plot pops up.
embedded the html plot into the Rmarkdown by using the following:
```{r, echo=FALSE}
htmltools::includeHTML("styled-scatter.html")
```
and here is the result: