pandas 如何在散景图中旋转 X 轴标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42354648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to rotate X-axis labels in bokeh figure?
提问by Liam Hanninen
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
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
是一个 Pandas 数据框,具有典型的、顺序的、非重复的索引。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'. 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?
如您所见,图表下方和 x 轴标题“功能”上方有一个难看的污迹。这是标签标题(技术上是矩形标题)。我如何为标签创建空间并可能旋转 45 度,以便它们可读而不仅仅是重叠的混乱?
回答by bluenote10
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:
为了将标签向左旋转 90 度,您可以设置major_label_orientation
为 π/2。这可以在创建轴元素时(如果您使用低级绘图作为轴构造函数的 kwarg)或在您创建绘图/图形后完成,例如:
p.xaxis.major_label_orientation = math.pi/2
See also this examplein the documentation.
另请参阅文档中的此示例。
回答by serv-inc
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.
也应该做你想做的。