Specify colors in a Gadfly (Julia) boxplot - colors

I am trying to reproduce this Seaborn plot using Gadfly.
The code I have so far is:
using CSV, DataFrames, Gadfly
download("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv", "tips.csv")
tips = DataFrame(CSV.File("tips.csv"));
plot(
tips,
x = :day,
y = :total_bill,
color = :smoker,
Geom.boxplot,
Scale.x_discrete(levels = ["Thur", "Fri", "Sat", "Sun"]),
Theme(
key_position = :top,
boxplot_spacing = 20px
),
)
I would like to specify the colors "green" and "purple" to match the Seaborn plot.
Any suggestions how to do this in Gadfly?
Additional:
How to set the smoker order from yes to no?

You need to add a line with Scale.color_discrete_manual:
using CSV, DataFrames, Gadfly
download(
"https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv",
"tips.csv",
)
tips = DataFrame(CSV.File("tips.csv"));
plot(
tips,
x = :day,
y = :total_bill,
color = :smoker,
Geom.boxplot,
Scale.x_discrete(levels = ["Thur", "Fri", "Sat", "Sun"]),
Scale.color_discrete_manual("purple", "green", order=[2, 1]),
Theme(key_position = :top, boxplot_spacing = 20px),
)

Related

How to set px.timeline tickvals?

I created a gantt chart using px.timeline but for some reason when i use color function it changes the order of my data and to keep similar colors together...
y axis is string/ category values and i tried manually setting it with a list in the order i'd like the y axis to be graphed but nothing changed.
fig = px.timeline(dfGantt,
x_start = 'START',
x_end = 'END',
y = 'ident',
color = 'color',
)
fig.update_yaxes(tickvals = dfGantt['ident'],
type='category',
autorange="reversed",
)
If i remove the color option it graphs as I'd like/expect.
This worked:
-using category_orders
fig = px.timeline(dfGantt,
x_start = 'START',
x_end = 'END',
y = 'ident',
color = 'color',
category_orders = {'ident':dfGantt['ident'].tolist()})
fig.update_yaxes(type='category',
)

Bokeh: Is there a way to set the styling for all figures at once?

I am coding for 20+ tabs to plot similar parameters and have been writing the styling for each figure separately. Is there a way to set the styling all at once for all figures?
p = figure(tools=TOOLS, x_axis_type='datetime',plot_height=400, plot_width=700,
outline_line_color = 'gray',
y_axis_label = 'Volts',
y_range = DataRange1d()
)
# Setting the second y axis range name and range
#p.extra_y_ranges = {"foo": Range1d(start=0, end=0.000006)}
p.extra_y_ranges = {"foo": DataRange1d()}
# Adding the second axis to the plot.
p.add_layout(LinearAxis(y_range_name="foo"), 'right')
a1=p.line(x='Time', y='340', line_color="darkcyan", line_width=1, source=source)
a11 = p.line(x='Time',y='StdDev', line_color = 'red', line_width=1, y_range_name="foo", source=source)
p.y_range.renderers=[a1]
p.extra_y_ranges['foo'].renderers = [a11]
tooltips=[( "Time", "#Time{%H:%M:%S}"),
("Y-value", "$y")]
formatt ={'Time':'datetime'}
p.add_tools(HoverTool(tooltips=tooltips, formatters=formatt, mode = 'mouse'))
p.title.text = "Plot Title "
p.title.text_color = "gray"
p.title.text_font = "arial"
p.title.text_font_style = "bold"
p.xgrid[0].grid_line_color=None
p.ygrid[0].grid_line_alpha=0.5
p.xaxis.axis_label = 'TimeStamp'
Following my way, I would essentially need to repeat above code 20+ times for each tab.

How do I add vertical moving hover line to my plotly chart

I am trying to achieve what is done here: https://www.quantalys.com/Fonds/120955 with javascript in python plotly. I want to add the hover vertical line and the red annotation on the x axis. I have done some searching on goolgle but I couldn't find the the answer I'm looking for. My current chart looks like this:
trace1 = go.Scatter(
x = df1.x,
y = df1.y,
name = "M&G OPTIMAL INCOME FD EUR AH ACC",
hoverinfo= 'name',
opacity=0.7,
mode = 'lines',
line = dict(
color = ('rgb(2, 12, 245)'),
width = 1,
),
)
trace2 = go.Scatter(
x = df2.x,
y = df2.y,
opacity=0.7,
name = "Alloc Flexible Prudent Monde",
hoverinfo= 'name',
mode = 'lines',
line = dict(
color = ('rgb(67, 45, 24)'),
width = 1,
)
)
trace3 = go.Scatter(
x = df3.x,
y = df3.y,
name = "25% MSCI World + 75% ML Global",
hoverinfo= 'name',
mode = 'lines',
opacity=0.7,
line = dict(
color = ('rgb(205, 12, 24)'),
width = 1,
)
)
layout = go.Layout(
xaxis=dict(
showline=True,
showgrid=True,
showticklabels=True,
linecolor='rgb(204, 204, 204)',
linewidth=2,
mirror=True,
),
yaxis=dict(
showline=True,
showgrid=True,
showticklabels=True,
linecolor='rgb(204, 204, 204)',
linewidth=2,
mirror=True,
),
showlegend=True,
)
data= [trace1, trace2,trace3]
fig = dict(data=data, layout=layout)
iplot(fig, filename='line-mode')
Add this to your layout definition.
showlegend = True,
hovermode = 'x'
Add this to your xaxis definition.
showspikes = True,
spikemode = 'across',
spikesnap = 'cursor',
showline=True,
showgrid=True,
...
And add this to your layout definition:
spikedistance = -1,
xaxis=dict(...
Please refer to this post and the documentation by plotly. :)
EDIT
You ask for the x-axis lable. Please use
spikemode = 'across+toaxis'
Additionally I would suggest to use
spikedash = 'solid'
because it is better fitting your example.

add labels and customize nodes in plotly sankey diagram

I am trying to customize a sankey diagram done with pyplot. I would simply like to make some nodes invisible (e.g. transparent) and add some labels to the connection between nodes.
I modified slightly the code given as an example in the website, without much success. The node properties seem to be the same for all the same. Adding a label to the links does not modify the output.
import plotly.plotly as py
data = dict(
type='sankey',
node = dict(
pad = 15,
thickness = 20,
line = dict(
color = "black",
width = 0.5
),
label = ["A1", "A2", "B1", "B2", "C1", "C2"],
color = ["blue", "blue", "blue", "blue",
"white", "white"]),#attempt to make it less visible
link = dict(
source = [0,1,0,2,3,3],
target = [2,3,3,4,4,5],
value = [8,4,2,8,4,2],
# attempt to add labels
label= [8,4,2,8,4,2]))
layout = dict(
title = "Basic Sankey Diagram",
font = dict(
size = 10
)
)
fig = dict(data=[data], layout=layout)
py.iplot(fig, validate=False)
Any suggestion on how to do it?
You could just add annotations for each of the link labels, the problem is, even if you add label to the links, it will get shown only on hover! below is an example of added annotations for your reference!
import plotly.graph_objs as go
import plotly.offline as py
py.init_notebook_mode()
import numpy as np
data = dict(
type='sankey',
node = dict(
pad = 15,
thickness = 20,
line = dict(
color = "black",
width = 0.5
),
label = ["A1", "A2", "B1", "B2", "C1", "C2"],
color = ["blue", "blue", "blue", "blue",
"gray", "white"]),#attempt to make it less visible
link = dict(
source = [0,1,0,2,3,3],
target = [2,3,3,4,4,5],
value = [8,4,2,8,4,2],
# attempt to add labels
label= [8,4,2,8,4,2]))
layout = dict(
title = "Basic Sankey Diagram",
font = dict(
size = 10
),
annotations=[
dict(
x=0.25,
y=0.75,
text='8',
showarrow=False
),
dict(
x=0.75,
y=0.25,
text='4',
showarrow=False
)
]
)
fig = dict(data=[data], layout=layout)
py.iplot(fig, validate=False)

Changing the attributes of the what appears when hovering over a Choropleth Map in plotly

I am using plotly in Python 3.6.3 and am trying to do a Choropleth map as in here. I would like to change the attributes of what appears when hovering above the map. That is, for example, if we consider the first map and hover of California, it looks like:
I want to change both the font size of the content that appears and the size of the box. Is there a way to access those?
Here is the code that generates it:
import plotly.plotly as py
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')
for col in df.columns:
df[col] = df[col].astype(str)
scl = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],\
[0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]
df['text'] = df['state'] + '<br>' +\
'Beef '+df['beef']+' Dairy '+df['dairy']+'<br>'+\
'Fruits '+df['total fruits']+' Veggies ' + df['total veggies']+'<br>'+\
'Wheat '+df['wheat']+' Corn '+df['corn']
data = [ dict(
type='choropleth',
colorscale = scl,
autocolorscale = False,
locations = df['code'],
z = df['total exports'].astype(float),
locationmode = 'USA-states',
text = df['text'],
marker = dict(
line = dict (
color = 'rgb(255,255,255)',
width = 2
) ),
colorbar = dict(
title = "Millions USD")
) ]
layout = dict(
title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)',
geo = dict(
scope='usa',
projection=dict( type='albers usa' ),
showlakes = True,
lakecolor = 'rgb(255, 255, 255)'),
)
fig = dict( data=data, layout=layout )
py.iplot( fig, filename='d3-cloropleth-map' )
The chloropleth>hoverlabel function lets you set the background color, border color, and font. The size of the border box is determined by the text within it, however. If the name shows up as truncated it can be expanded with the chloropleth>hoverlabel>namelength function.

Resources