change Bar Chart's ColorAttr in Bokeh 0.11 - bokeh

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]]

Related

Define custom color_palette in Julia Plots package

I am currently using Plots package in Julia with pyplot as the backend. I can choose color_palette and make plots by
pyplot(color_palette=:delta)
Plots.plot(x, y)
What is the syntax to define and use a custom color palette according to, for example, Okade and Ito below, for color-blind-friendliness? Thanks!
Just pass a Vector of RGB - see the docs here https://docs.juliaplots.org/latest/colors/#Misc-1 But that particular palette is actually built into Plots - so you can get it by specifying theme(:wong2) before plotting, it will change the palette and color gradient for the duration of the session.

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)

Calculate distinguishable color palette in R

I am using R to create a plot that contains 20 distinct groups, and I would like to color each of them differently. I am also familiar with Matlab and when working with that program I have found that "distinguishable_colors" matlab file worked best for distinguishing different colors 1. I have looked at the "rainbow", "rainbow_hcl", and "brewer" palettes, but none of them look as good as "distinguishable_colors.mat". I am wondering if anyone knows of a function in R that will create the same palette as the "distinguishable_colors.mat" matlab function?
That function in MatLab seems to iteratively search over RBG space for sets of color which are maximally different from each other. I don't know of such a thing in R, but we can come pretty close.
We can find a color palette of a few colors which suits our needs (perhaps using http://colorbrewer2.org/) and use those seed color to create a colorRampPalette for any number of colors.
pal<-colorRampPalette(c('#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00'))
N=10
plot(rnorm(N),rnorm(N),pch=16,col=pal(N),cex=3)
The R package Polychrome provides tools for qualitative palettes with many (20 or more) colors. It comes with two vignettes giving the provided palettes and tools for creating palettes.

Plot different colours - Scilab

Is it possible to have different colours in my plot in Scilab? I use the mtlb_hold to hold the graph and it works fine, but my problem is that then I have the same colours in my graph. In Matlab with the hold command, I have different colours. Is it possible to have different colours in Scilab too?
Thank you in advance.
Just found it. In the plot function, for example plot(), you can pass a second argument which specifies the color that will be used. For example, you can use use b for blue color, g for green, r for red and call plot() like this: plot(z,"r").
The SciLab documentation provides examples for using colors:
https://help.scilab.org/doc/5.3.3/en_US/color.html
The plot2d()-function e.g. accepts an attribute style, where you can even specify a color for each function with its full name:
plot2d(x,[sin(x),cos(x)],style=[color("red"),color("green")]);

How can I hide the axes in matplotlib 3d?

How can I make a 3D plot without showing the axes?
When plotting a 3d plot, Matplotlib not only draws the x, y, and z axes, it draws light gray grids on the x-y, y-z, and x-z planes. I would like to draw a "free-floating" 3D plot, with none of these elements.
Stuff I've tried:
# Doesn't work; this hides the plot, not the axes
my_3d_axes.set_visible(False)
# Doesn't do anything. Also, there's no get_zaxis() function.
my_3d_axes.get_xaxis().set_visible(False)
my_3d_axes.get_yaxis().set_visible(False)
Ben Root provided a patch that fixes this for 1.0.1. It can be found as an attachment to the last email of this thread. To quote Ben:
Ok, looks like the hiding of the 3d axes was a feature added after the v1.0 release (but before I started working on mplot3d). This patch should enable the basic feature without interfering with existing functions. To hide the axes, you would have to set the private member "_axis3don" to False, like so:
ax = plt.gca(projection='3d')
ax._axis3don = False
If you do it this way, then you will get what you want now, and your code will still be compatible with mplot3d when you upgrade (although the preferred method would be to call set_axis_on() or set_axis_off()).
I hope that helps!
Ben Root
ax.set_axis_off()
Just to provide a concrete and direct example of what was mentioned at https://stackoverflow.com/a/7363931/895245
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import mpl_toolkits.mplot3d.art3d as art3d
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_axis_off()
# Draw a circle on the x=0 'wall'
p = Circle((0, 0), 1, fill=False)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, zdir="x")
p = Circle((0, 0), 1, fill=False)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, zdir="z")
ax.set_xlim(-1.2, 1.2)
ax.set_ylim(-1.2, 1.2)
ax.set_zlim(-1.2, 1.2)
plt.savefig('main.png', format='png', bbox_inches='tight')
Output:
Without ax.set_axis_off() it would look like:
You will notice however that this produces an excessively large whitespace margin around the figure as it simply hides the axes but does not change the viewbox. I tried bbox_inches='tight' and it did not help as it does in 2D. How to solve that at: Remove white spaces in Axes3d (matplotlib)
Tested on matplotlib==3.2.2.

Resources