I have a Figure made up of 4 subplots, each subplot looks like this:
Data:Histogram({
'autobinx': False,
'histnorm': 'probability density',
'legendgroup': 'HOM_TP',
'marker': {'color': '#eb8909'},
'name': 'HOM_TP',
'opacity': 0.7,
'x': array([99., 99., 99., ..., 99., 99., 99.]),
'xaxis': 'x',
'xbins': {'end': 99.0, 'size': 0.0, 'start': 99.0},
'yaxis': 'y'
})
Layout({
'annotations': [{'showarrow': False,
'text': 'TEXT',
'x': 2.5,
'xanchor': 'left',
'xref': 'x',
'y': 1,
'yanchor': 'top',
'yref': 'y domain'}],
'shapes': [{'line': {'color': 'green', 'dash': 'dash', 'width': 3},
'type': 'line',
'x0': 2.5,
'x1': 2.5,
'xref': 'x',
'y0': 0,
'y1': 1,
'yref': 'y domain'}],
'sliders': [{'active': 10,
'currentvalue': {'prefix': 'Frequency: '},
'pad': {'t': 50},
'steps': [{'args': [{'visible': [True]}, {'title': 'Slider switched to step: 0'}], 'method': 'update'}]}],
'template': '...'
})
I can add each plot to the main Figure using add_trace(subplot.data) but this does not bring the layout info (sliders and vertical line). How can I add the layout to the subplots within the main figure?
Current code:
def make_tiled_figure(subfigs, metric):
'''
Take list of figures ( figure factory plot objects) to be combined into
tiled image. Return single figure object with tiled subplots.
'''
fig = make_subplots(rows=1, cols=4, subplot_titles=[
'SNP', 'INDEL', 'HET', 'HOM'])
# decide on position and add subfigures to plot
for i, subfig in enumerate(subfigs):
if subfig:
for trace in subfig.data:
fig.add_trace(trace, row=1, col=i+1)
fig.update_layout(subfig.layout)
# specify plot size and title
fig.update_layout(height=500, width=1800, title_text=metric)
return fig
My eventual goal is to have a vertical line that can slide across each subplot, controlled by the sliders (essentially by having multiple lines made visible/invisible by the slider position). If there is a better way of achieving this I am very open to suggestions.
Related
I have created a gauge chart but I want to mention the meaning of labels, like 0 = low and 5 = high. that means I will need two labels (low on the left and high on the right).
Here is how my graph look like:
code:
import plotly.graph_objects as go
fig = go.Figure(go.Indicator(
domain = {'x': [0, 1], 'y': [0, 1]},
value = 4.3,
mode = "gauge+number+delta",
title = {'text': "General satisfaction"},
delta = {'reference': 2.5},
gauge = {'axis': {'range': [None, 5], 'tickwidth': 1,'tickcolor': "black"},
'bar': {'color': "MidnightBlue"},
'steps' : [
{'range': [0, 1], 'color': "DarkTurquoise"},
{'range': [1, 2], 'color': "MediumTurquoise"},
{'range': [2, 3], 'color': "Turquoise"},
{'range': [3, 4], 'color': "PaleTurquoise"},
{'range': [4, 5], 'color': "lightcyan"}],
'threshold' : {'line': {'color': "brown", 'width': 4}, 'thickness': 0.75, 'value': 4.8}}))
fig.show()
Is there any parameter that can help me in this case?
graph objects indicator tickmode, tickvals and ticktext
demonstrated below
fig.update_traces(
gauge={
"axis": {
"tickmode": "array",
"tickvals": list(range(6)),
"ticktext": ["0 - low" if i == 0 else "5 - high" if i==5 else i for i in range(6)],
}
}
)
I am trying to order 4 dictionary lists from lowest to highest and I am invalid syntax (I am new to bioinformatics)
I have tried inline sorting
lists = sorted(list_dct.items, key=lambda k: k['name'])
list_dct = [{'name': 0.5, 0, 0, 0.5},
{'name' : 0.25, 0.25, 0.25, 0.25},
{'name' : 0, 0, 0, 1},
{'name' : 0.25, 0, 0.5, 0.25}]
print(lists)
I am getting an invalid syntax message... I should get the lists sorted by row lowest to row highest
You need to construct your dictionaries correctly. I've chosen to make the values a list. Then sort them with a list comprehension:
list_dct = [{'name': [0.5, 0, 0, 0.5]},
{'name' : [0.25, 0.25, 0.25, 0.25]},
{'name' : [0, 0, 0, 1]},
{'name' : [0.25, 0, 0.5, 0.25]}]
sorted([ d.get('name') for d in list_dct ])
1.) Define list_dct before the sorted() function, otherwise it's syntax error
2.) You want to sort whole list_dct, not list_dct.items()
3.) Make custom key= sorting function, where from each item we're sorting we select 'name' key.
list_dct = [{'name': [0.5, 0, 0, 0.5]},
{'name' : [0.25, 0.25, 0.25, 0.25]},
{'name' : [0, 0, 0, 1]},
{'name' : [0.25, 0, 0.5, 0.25]}]
lists = sorted(list_dct, key=lambda k: k['name'])
from pprint import pprint
pprint(lists)
Prints:
[{'name': [0, 0, 0, 1]},
{'name': [0.25, 0, 0.5, 0.25]},
{'name': [0.25, 0.25, 0.25, 0.25]},
{'name': [0.5, 0, 0, 0.5]}]
I am making a pie chart that looks like this.
I want to make multiple pie charts for different sets of data and keep the colours fixed to the legend names, but when the order changes, the colour scheme follows the order.
Is there a way to pass a dict into the chart to fix the colours to specific items?
[]
You cannot pass a dictionary with your colors, but you can specify the colors manually, set sort to False and pass the values always in the same order, e.g.
import plotly
fig = {
'data': [{'labels': ['Residential', 'Non-Residential', 'Utility'],
'values': [19, 26, 55],
'type': 'pie',
'sort': False,
'marker': {'colors': ['rgb(255, 0, 0)',
'rgb(0, 255, 0)',
'rgb(0, 0, 255)']
}
}]
}
fig = {
'data': [{'labels': ['Residential', 'Non-Residential', 'Utility'],
'values': [100, 10, 25],
'type': 'pie',
'sort': False,
'marker': {'colors': ['rgb(255, 0, 0)',
'rgb(0, 255, 0)',
'rgb(0, 0, 255)']
}
}]
}
plotly.offline.plot(fig)
I have a dataset with about 9800 entries. One column contains user names (about 60 individual user names). I want to generate a scatter plot in matplotlib and assign different colors to different users.
This is basically what I do:
import matplotlib.pyplot as plt
import pandas as pd
x = [5, 10, 20, 30, 5, 10, 20, 30, 5, 10, 20, 30]
y = [100, 100, 200, 200, 300, 300, 400, 400, 500, 500, 600, 600]
users =['mark', 'mark', 'mark', 'rachel', 'rachel', 'rachel', 'jeff', 'jeff', 'jeff', 'lauren', 'lauren', 'lauren']
#this is how the dataframe basicaly looks like
df = pd.DataFrame(dict(x=x, y=y, users=users)
#I go on an append the df with colors manually
#I'll just do it the easy albeit slow way here
colors =['red', 'red', 'red', 'green', 'green', 'green', 'blue', 'blue', 'blue', 'yellow', 'yellow', 'yellow']
#this is the dataframe I use for plotting
df1 = pd.DataFrame(dict(x=x, y=y, users=users, colors=colors)
plt.scatter(df1.x, df1.y, c=df1.colors, alpha=0.5)
plt.show()
However, I don't want to assign colors to the users manually. I have to do this many times in the coming weeks and the users are going to be different every time.
I have two questions:
(1) Is there a way to assign colors automatically to the individual users?
(2) If so, is there a way to assign a color scheme or palette?
user_colors = {}
unique_users = list(set(users))
step_size = (256**3) // len(unique_users)
for i, user in enumerate(unique_users):
user_colors[user] = '#{}'.format(hex(step_size * i)[2:])
Then you've got a dictionary (user_colors) where each user got one unique color.
colors = [user_colors[user] for user in users]
Now you've got your array with a distinct color for each user
i am searching for an answer but i didn't find anything about my problem.
x=[['100',220, 0.5, 0.25, 0.1],['105',400, 0.12, 0.56, 0.9],['600',340, 0.4, 0.7, 0.45]]
y=[['1','100','105','601'],['2','104','105','600'],['3','100','105','604']]
i want as result:
z=[['1','100',0.5,0.25,0.1,'105',0.12,0.56,0.9],['2','105',0.12,0.56,0.9,'600',0.4,0.7,0.45],['3','100',0.5, 0.25, 0.1,'105', 0.12, 0.56, 0.9]]
i want to search in list y and match list x with list y where i get a new list z that containts the common sublists.
this is just an example, normally contains list x and y 10000 sublists.
i compare out of y ['1','100','105','601'] and search the '100','105','601' in list x (example ['100',220, 0.5, 0.25, 0.1]). if i find a match i make a new list z.
Can someone help me?
Answer edited because comments
You said in the comments:
search the second, third and fourth number in each y. and compare that with the number on place one in list x
and
then i would like to add (from list x) the numbers on place 1,3,4,5
Then try something like this:
x = [
['100', 220, 0.5, 0.25, 0.1],
['105', 400, 0.12, 0.56, 0.9],
['600', 340, 0.4, 0.7, 0.45]
]
y = [
['1', '100', '105', '601'],
['2', '104', '105', '600'],
['3', '100', '105', '604']
]
z = []
xx = dict((k, v) for k, _, *v in x)
for first, *yy in y:
zz = [first]
for n in yy:
numbers = xx.get(n)
if numbers:
zz.append(n)
zz.extend(numbers)
z.append(zz)
print(z)
z should now be:
[['1', '100', 0.5, 0.25, 0.1, '105', 0.12, 0.56, 0.9],
['2', '105', 0.12, 0.56, 0.9, '600', 0.4, 0.7, 0.45],
['3', '100', 0.5, 0.25, 0.1, '105', 0.12, 0.56, 0.9]]
First, I convert x into a dictionary, for easy lookup.
The iteration pattern used here was introduced with pep-3132 and works like this:
>>> head, *tail = range(5)
>>> head
0
>>> tail
[1, 2, 3, 4]