R + Import picture in a graph panel - r

I have a three-element (nrow=1, ncol=3) panel, with a single caption across, to illustrate a point. Two elements are graphs. The third one has to be a picture, which I need to import from public sources. Can you guide me on how to import this picture? Many thanks.

Related

Jupiter notebook is printing words before plotting my images (not correct order)

In my code, I have it so that first images are plotted and then the user is asked to choose an image.
But when I run the cell, the user is first asked to choose an image and then the images are plotted. Not sure why.
Code in this picture:
Jupyter will render the plot after everthing else if it is running in 'inline' mode. I can't reproduce what you have exactly but take the following example:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,10)
y = 5*x +2
plt.plot(x,y)
print('Text is here first even though the plot call comes last')
The output of this looks like:
Plot example without inline
So the plot is rendered last. Instead if we add '%matplotlib notebook' to the start. The plot will be rendered interactively and before the text. If you want to switch back just change it to '%matplotlib inline'. There are a number of other backend plotting options that are worth exploring.

Select data from a Datashader plot

I'm using Datashader to make crossplots of different parameters due to the quantity of data I'm working with. My idea is to enable some kind of interaction that allows the user to select directly from the plot, the outliers based on the data's tendency showed these ones. I wonder if Holoviews Selection1D stream is compatible with datashader.
This code simulates what I meant:
import numpy as np
import holoviews as hv
from holoviews import opts
from holoviews import streams
from holoviews.operation.datashader import datashade
hv.extension('bokeh')
# Tools to select data
opts.defaults(opts.Points(tools=['box_select', 'lasso_select']))
# Random points to plot
random_points = hv.Points(np.random.randn(1000))
# Holoviews
selection = streams.Selection1D(source=random_points)
# Selected points by Holoviews selection stream
selected_box = hv.DynamicMap(lambda index: random_points.iloc[index],
kdims=[], streams=[selection])
# Final Overlay
Overlay = (random_points * selected_box).opts(padding = 0.01)
Overlay
So far so good, whenever I call selection I get a matrix compounded by the index of the selected points. After Calling the Datashader plot with datashade(Overlay) , the interaction between random_points and selected_box breaks, therefore I'm not sure if this stream is compatible with datashader or if the way I use the stream is wrong!
This is what inspired my idea:
http://holoviews.org/reference/apps/bokeh/selection_stream.html#bokeh-gallery-selection-stream
Helpful example to see the possibilities. Now I know it's not necessary to extract the data from the plot... I could just do another plot using Dynamic maps and streams, either by bounds or selection1D.

different element sizes with matplotlib backend

I'm trying to accomplish a heatmap color bar to add extra info about the kdims in my heatmap. (Like the colSideColors option if you are familiar with R's heatmap.2 package.)
I can get a nice result with bokeh backend, but don't know how to get custom (different) element sizes when using matplotlib backend.
Can anyone tell me how to make the strip plot "shorter" (less "high") in the matplotlib backend example?
Setup
import pandas as pd
import numpy as np
import holoviews as hv
hv.extension('bokeh', 'matplotlib')
# dummy data
samples = ['sample{}'.format(x) for x in range(5)]
df = pd.DataFrame(np.random.rand(5, 5),columns=samples, index=samples).reset_index()
df = df.melt(id_vars='index', var_name='y').rename(columns={'index': 'x'})
# column means
df_strip = df.groupby('x').mean().reset_index()
df_strip['y'] = 'dummy'
# make plots
heatmap = hv.HeatMap(df, kdims=['x','y'])
strip = hv.HeatMap(df_strip, kdims=['x','y'])
Result with bokeh
%%output size=100 backend='bokeh'
(strip.options(xaxis=None, yaxis=None, height=50) +
heatmap.options(xrotation=90)).cols(1)
Result with matplotlib backend
%%output size=100 backend='matplotlib'
%%opts Layout [sublabel_format='' vspace=0.1]
(strip.options(xaxis=None, yaxis=None, aspect=1) +
heatmap.options(xrotation=90, aspect=1)).cols(1)
hv.__version__
'1.10.8'
The sizing unfortunately works very differently in the two backends, which means it can be somewhat difficult to get the same behavior. In this particular case you will want to set a larger aspect on the strip plot while also telling the Layout that it should weight the aspect when computing the size of the plots. Doing that looks something like this:
%%output size=100 backend='matplotlib'
%%opts Layout [sublabel_format='' vspace=0.1 aspect_weight=1]
(strip.options(xaxis=None, yaxis=None, aspect=5) +
heatmap.options(xrotation=90, aspect=1)).cols(1)

Custom art in NetworkX graph

Is anyone aware of a way to put in an image (vector or raster) in place of a text label for a node or edge in a NetworkX graph visualization?
I prefer a solution that uses the matplotlib plot engine rather than the graphviz, but will take either solution.
In principle, the below should work. I transform the points into pixel coordinates, and then use figimage to put the image at that point.
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.image as image
im = image.imread('Lower_case_a.png')
G=nx.fast_gnp_random_graph(2,1)
pos = nx.spring_layout(G)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.patch.set_alpha(0)
ax.axis(xmin=-1,xmax=2,ymin=-1,ymax=2)
for node in G.nodes():
x,y = pos[node]
trans_x, trans_y = ax.transData.transform((x,y))
fig.figimage(im,trans_x,trans_y) #could use custom image for each node
nx.draw_networkx_edges(G,pos)
plt.savefig('tmp.png')
It almost works for me. I get:
I think that's because of some weird issue on my computer. When I run the code provided at matplotlib.org to explain transData.transform, it should give me, but instead I get
So I feel like the offset I'm seeing may be a problem on my machine and I'd like to try another computer. At any rate, let me know if this works for you and hopefully it at least points you in the right direction.

change Bar Chart's ColorAttr in Bokeh 0.11

Note from maintainers: This question as originally posed concerns the obsoleted and removed bokeh.charts API. See Handling Categorical Data for information on Bar cahrts in modern Bokeh
The default color palette used in Bokeh's Bar chart's ColorAttr only has six elements and that poses obvious limitation. Supposedly one can change the Colorattr helper function with same a different palette like Spectral10
But I have a hard time figuring how to apply that to an actual chart. Does anyone have an example of how to do assign a new palette like Spectral10 to the ColorAttr function and then assign the new ColorAttr to a Bar char?
Thanks!
SH
It is actually straightforward, although it is certainly not easy to find in the documentation. You assign the palette with an argument in the Bar chart call:
from bokeh.charts import Bar
from bokeh.palettes import Spectral10
p=Bar( ... ,palette=Spectral10)
If it may be of any use, here is also a code snippet (adapted from other contributors) that I use to generate palettes with an arbitrary number of colors:
import matplotlib.cm as cm
import numpy as np
colormap =cm.get_cmap("jet")
different_colors=15
color_mapping=colormap(np.linspace(0,1,different_colors),1,True)
bokeh_palette=["#%02x%02x%02x" % (r, g, b) for r, g, b in color_mapping[:,0:3]]

Resources