Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
790 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...