Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate X-axis labels in bokeh figure?

I'm just starting to use Bokeh. Below I create some args I use for the rect figure.

x_length = var_results.index * 5.5 

Multiplying the index by 5.5 gave me more room between labels.

names = var_results.Feature.tolist() y_length = var_results.Variance y_center = var_results.Variance/2 

var_results is a Pandas dataframe that has a typical, sequential, non-repeating index. var_results also has a column Features that is strings of non-repeated, names, and finally it has a column Variance which is dtype float.

r = figure(x_range = names,             y_range = (-0.05,.3),             active_scroll = 'wheel_zoom',             x_axis_label = 'Features',             y_axis_label = 'Variance')    r.rect(x_length,         y_center,         width=1,         height=y_length,         color = "#ff1200") output_notebook() show(r) 

I'm essentially making a bar chart with rectangles. Bokeh seems to be very customizable. But my graph looks rough around the edges, literally.

enter image description here

As you can see there is an ugly smudge just below the chart and above the x-axis title 'Features'. This is the label titles (technically the rectangle titles). How do I create space for and perhaps rotate to 45 degrees the labels so that they are readable and not just an overlapping mess?

like image 372
Liam Hanninen Avatar asked Feb 20 '17 21:02

Liam Hanninen


People also ask

How do you rotate the X-axis labels?

Rotate X-Axis Tick Labels in Matplotlib There are two ways to go about it - change it on the Figure-level using plt. xticks() or change it on an Axes-level by using tick. set_rotation() individually, or even by using ax.

How do you rotate the X-axis labels in Matlab?

ang = xtickangle returns the rotation angle for the x-axis tick labels of the current axes as a scalar value in degrees. Positive values indicate counterclockwise rotation. Negative values indicate clockwise rotation. ang = xtickangle( ax ) uses the axes specified by ax instead of the current axes.


2 Answers

In order to rotate the labels e.g. by 90 degrees to the left, you can set major_label_orientation to π/2. This can be done either when creating the axis element (as a kwarg to the axis constructor if you are using low level plotting) or also after you have created a plot/figure, for instance by:

p.xaxis.major_label_orientation = math.pi/2  # or alternatively: p.xaxis.major_label_orientation = "vertical" 

See also this example in the documentation.

like image 96
bluenote10 Avatar answered Sep 18 '22 15:09

bluenote10


As an alternative to rotation, you set the orientation to a fixed value:

p.xaxis.major_label_orientation = "vertical" 

should do what you want, too.

like image 40
serv-inc Avatar answered Sep 17 '22 15:09

serv-inc