简体   繁体   English

如何在散景图中旋转 X 轴标签?

[英]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.将索引乘以 5.5 给了我更多标签之间的空间。

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是一个 Pandas 数据帧,它具有典型的、顺序的、非重复的索引。 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. var_results也有一个列Features ,它是非重复的名称字符串,最后它有一个列Variance ,它是 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.但从字面上看,我的图表边缘看起来很粗糙。

在此处输入图片说明

As you can see there is an ugly smudge just below the chart and above the x-axis title 'Features'.如您所见,图表下方和 x 轴标题“功能”上方有一个难看的污迹。 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?我如何为标签创建空间并可能旋转 45 度,以便它们可读而不仅仅是重叠的混乱?

In order to rotate the labels eg by 90 degrees to the left, you can set major_label_orientation to π/2.为了将标签向左旋转 90 度,您可以将major_label_orientation设置为 π/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:这可以在创建轴元素时(如果您使用低级绘图作为轴构造函数的 kwarg)或在您创建绘图/图形后完成,例如:

p.xaxis.major_label_orientation = math.pi/2

# or alternatively:
p.xaxis.major_label_orientation = "vertical"

See also this example in the documentation.另请参阅文档中的此示例

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.也应该做你想做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM