如何移动 plotnine 中的轴标签?
How do I move the axis labels in plotnine?
我正在尝试将 x 和 y 轴标签移动到图表左下角彼此靠近的位置。
这在 R 的 ggplot2 中很简单:
theme(
axis.title.y = element_text(angle = 0, vjust = 0, hjust = 0.5, size = 15,
color = "grey55"),
axis.title.x = element_text(hjust = 0, size = 15, color = "grey55"))
但是,它会导致 plotnine 中出现意外行为,其中标签仅使用标准参数移动几毫米。此代码示例使用 plotnine 项,但会导致相同的奇怪行为。
p1 = [39.5,
117.2,
129.4,
0.7,
87.2,
164.5,
224.0,
110.7,
121.0,
191.1,
4.1,
104.6,
125.7,
136.2,
202.5,
76.3,
216.8,
19.6,
75.3,
120.2,
287.6,
237.4,
97.2,
248.8,
67.8,
151.5,
116.0,
213.5,
188.4,
265.2]
p1_1 = pd.DataFrame(p1, columns = ['Test'])
# Histogram
(
p9.ggplot(p1_1, p9.aes(x = 'Test'))
+ p9.geom_histogram(bins = N/6, fill = "#f9d9d6", color = "#E34234")
+ p9.theme_classic()
+ p9.themes.theme(
axis_title_y = p9.themes.element_text(va = 'bottom', angle = 0, ha = 'center', size = 15, color = "#8c8c8c"),
axis_title_x = p9.themes.element_text(hjust = 0, size = 15, color = "#8c8c8c"),
axis_text = p9.themes.element_text(size = 12, color = "#999999"),
axis_line = p9.themes.element_line(color = "#999999"),
axis_ticks = p9.themes.element_line(color = "#999999"),
plot_title = p9.themes.element_text(hjust = 0.5, size = 40, color = "#666666"),
panel_grid = p9.themes.element_blank(),
)
).draw();
在 this 对有关 plot_title
的相关 GitHub 问题发表评论后,您可以通过设置 x
和 y
的位置来改变位置轴标题。对于您想要的结果,我认为 y=.1
和 x=.125
工作正常:
(p9.ggplot(p1_1, p9.aes(x = 'Test'))
+ p9.geom_histogram(bins = 10, fill = "#f9d9d6", color = "#E34234")
+ p9.theme_classic()
+ p9.themes.theme(
axis_title_y = p9.themes.element_text(angle = 0, va = 'bottom', ha = 'right',
size = 15, color = "#8c8c8c", y = .1),
axis_title_x = p9.themes.element_text(ha = 'left', va = 'center', size = 15,
color = "#8c8c8c", x = 0.125),
axis_text = p9.themes.element_text(size = 12, color = "#999999"),
axis_line = p9.themes.element_line(color = "#999999"),
axis_ticks = p9.themes.element_line(color = "#999999"),
plot_title = p9.themes.element_text(hjust = 0.5, size = 40, color = "#666666"),
panel_grid = p9.themes.element_blank(),
)
)
我正在尝试将 x 和 y 轴标签移动到图表左下角彼此靠近的位置。
这在 R 的 ggplot2 中很简单:
theme(
axis.title.y = element_text(angle = 0, vjust = 0, hjust = 0.5, size = 15,
color = "grey55"),
axis.title.x = element_text(hjust = 0, size = 15, color = "grey55"))
但是,它会导致 plotnine 中出现意外行为,其中标签仅使用标准参数移动几毫米。此代码示例使用 plotnine 项,但会导致相同的奇怪行为。
p1 = [39.5,
117.2,
129.4,
0.7,
87.2,
164.5,
224.0,
110.7,
121.0,
191.1,
4.1,
104.6,
125.7,
136.2,
202.5,
76.3,
216.8,
19.6,
75.3,
120.2,
287.6,
237.4,
97.2,
248.8,
67.8,
151.5,
116.0,
213.5,
188.4,
265.2]
p1_1 = pd.DataFrame(p1, columns = ['Test'])
# Histogram
(
p9.ggplot(p1_1, p9.aes(x = 'Test'))
+ p9.geom_histogram(bins = N/6, fill = "#f9d9d6", color = "#E34234")
+ p9.theme_classic()
+ p9.themes.theme(
axis_title_y = p9.themes.element_text(va = 'bottom', angle = 0, ha = 'center', size = 15, color = "#8c8c8c"),
axis_title_x = p9.themes.element_text(hjust = 0, size = 15, color = "#8c8c8c"),
axis_text = p9.themes.element_text(size = 12, color = "#999999"),
axis_line = p9.themes.element_line(color = "#999999"),
axis_ticks = p9.themes.element_line(color = "#999999"),
plot_title = p9.themes.element_text(hjust = 0.5, size = 40, color = "#666666"),
panel_grid = p9.themes.element_blank(),
)
).draw();
在 this 对有关 plot_title
的相关 GitHub 问题发表评论后,您可以通过设置 x
和 y
的位置来改变位置轴标题。对于您想要的结果,我认为 y=.1
和 x=.125
工作正常:
(p9.ggplot(p1_1, p9.aes(x = 'Test'))
+ p9.geom_histogram(bins = 10, fill = "#f9d9d6", color = "#E34234")
+ p9.theme_classic()
+ p9.themes.theme(
axis_title_y = p9.themes.element_text(angle = 0, va = 'bottom', ha = 'right',
size = 15, color = "#8c8c8c", y = .1),
axis_title_x = p9.themes.element_text(ha = 'left', va = 'center', size = 15,
color = "#8c8c8c", x = 0.125),
axis_text = p9.themes.element_text(size = 12, color = "#999999"),
axis_line = p9.themes.element_line(color = "#999999"),
axis_ticks = p9.themes.element_line(color = "#999999"),
plot_title = p9.themes.element_text(hjust = 0.5, size = 40, color = "#666666"),
panel_grid = p9.themes.element_blank(),
)
)