You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyX Python:如何调整颜色栏刻度数值的字体大小?

PyX颜色栏刻度字体大小调整方案

现有代码

g = graph.graphxy(height=8, width=8,
              x=graph.axis.linear(min=re_min, max=re_max, title=r"$\Re(c)$"),
              y=graph.axis.linear(min=im_min, max=im_max, title=r'$\Im(c)$'))
g.plot(graph.data.points(d, x=1, y=2, color=3, title="iterations"),
   [graph.style.density(gradient=color.rgbgradient.Rainbow)])
g.writeEPSfile()

运行上述代码可生成对应图表:
图表示例

技术问题

如何调整颜色栏中0、2、4、6、8、10这些刻度数值的字体大小,使其显示更大?


解决方法

要调整密度图颜色栏的刻度字体大小,需通过graph.style.density的coloraxis参数自定义轴样式,具体实现如下:

方式1:预定义自定义颜色轴

先创建配置好字体大小的颜色轴对象,再传入密度样式:

from pyx import graph, color

# 定义自定义颜色轴,设置刻度标签为大号字体
custom_color_axis = graph.axis.linear(
    tickattrs=[graph.axis.tick.labelattrs(fontsize=graph.axis.tick.sizelarge)]
)

# 初始化图表(原代码部分不变)
g = graph.graphxy(height=8, width=8,
              x=graph.axis.linear(min=re_min, max=re_max, title=r"$\Re(c)$"),
              y=graph.axis.linear(min=im_min, max=im_max, title=r'$\Im(c)$'))

# 绘图时传入自定义颜色轴
g.plot(graph.data.points(d, x=1, y=2, color=3, title="iterations"),
       [graph.style.density(gradient=color.rgbgradient.Rainbow, coloraxis=custom_color_axis)])

g.writeEPSfile()

方式2:直接在密度样式内配置

也可以直接在density样式中内嵌颜色轴的配置,省去单独定义对象的步骤:

g.plot(graph.data.points(d, x=1, y=2, color=3, title="iterations"),
       [graph.style.density(
           gradient=color.rgbgradient.Rainbow,
           coloraxis=graph.axis.linear(
               tickattrs=[graph.axis.tick.labelattrs(fontsize=12)]  # 直接指定字体大小数值
           )
       )])

字体大小选项

可以使用PyX预定义的字体大小常量,或直接指定数值:

  • graph.axis.tick.sizesmall:小号字体
  • graph.axis.tick.sizemedium:默认中号字体
  • graph.axis.tick.sizelarge:大号字体
  • 自定义数值:如fontsize=12(单位为pt)

内容的提问来源于stack exchange,提问作者Nigel1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 17:29:58