Skip to content

Chinese garbled code #3220

Closed
Closed

Description

@agentUD

Description of the problem

I've been using pygmt to draw recently, and found that Chinese fonts are messy, GMT supports Chinese fonts normally, but PYGMT is messy, how can I solve this problem? Thank you for your answer.

Minimal Complete Verifiable Example

No response

Full error message

No response

System information

PyGMT information:
  version: v0.9.0
System information:
  python: 3.8.17 | packaged by conda-forge | (default, Jun 16 2023, 07:01:59) [MSC v.1929 64 bit (AMD64)]
  executable: D:\SoftWare\Work_SoftWare\miniconda3\envs\Fluent-Qt\python.exe
  machine: Windows-10-10.0.19045-SP0
Dependency information:
  numpy: 1.24.4
  pandas: 1.5.3
  xarray: 2023.1.0
  netCDF4: 1.6.4
  packaging: 23.1
  contextily: None
  geopandas: None
  ghostscript: 9.56.1
GMT library information:
  binary version: 6.4.0
  cores: 12
  grid layout: rows
  image layout:
  library path: D:/SoftWare/Work_SoftWare/miniconda3/envs/Fluent-Qt/Library/bin/gmt.dll
  padding: 2
  plugin dir: D:/SoftWare/Work_SoftWare/miniconda3/envs/Fluent-Qt/Library/bin/gmt_plugins
  share dir: D:/SoftWare/Work_SoftWare/GMT6.4/share
  version: 6.4.0

Activity

  1. added
    bug
    on May 1, 2024
  2. welcome commented on May 1, 2024

    @welcome

    👋 Thanks for opening your first issue here! Please make sure you filled out the template with as much detail as possible. You might also want to take a look at our contributing guidelines and code of conduct.

  3. yvonnefroehlich commented on May 1, 2024

    @yvonnefroehlich
    Member

    @agentUD can you please provided code examples for an incorrectly working PyGMT case and a correctly working GMT case.

  4. CUMT530849827 commented on Dec 8, 2025

    @CUMT530849827

    @agentUD Hello, may I ask if you have solved the above problem? I also encountered the same issue. Could you please share how you solved it? Thank you very much. I feel that pygmt cannot obtain the path of the Chinese font, so it's garbled

  5. seisman commented on Dec 8, 2025

    @seisman
    Member

    Below is a Pyton script modified from the post by the GMT-China community: https://docs.gmt-china.org/latest/chinese/unix/. Please note that setting pygmt.config(PS_CONVERT="C-I/home/xxxx/.gmt") doesn't work as expected (I don't know the reason yet). Instead, you need to add gs_option when calling Figure.show().

    import pygmt
    
    fig = pygmt.Figure()
    pygmt.config(FONT_TITLE="25p,41,black", FONT_LABEL="15p,39,black")
    fig.basemap(
        region=[0, 8, 0, 4],
        projection="X12c/4c",
        frame=["xaf+l'X轴'", "yaf+l'Y轴'", "WSen+t'中文标题'"],
    )
    fig.text(
        x=[2, 2, 2, 2, 4, 5, 6, 7],
        y=[3.5, 2.5, 1.5, 0.5, 3.5, 3.5, 3.5, 3.5],
        font=[
            "25p,39,black",
            "25p,40,blue",
            "25p,41,red",
            "25p,42,green",
            "25p,43,black",
            "25p,44,blue",
            "25p,45,red",
            "25p,46,green",
        ],
        text=[
            "中文宋体",
            "中文仿宋",
            "中文黑体",
            "中文楷体",
            "中文宋体",
            "中文仿宋",
            "中文黑体",
            "中文楷体",
        ],
    )
    fig.savefig("map.pdf", gs_option="-I/home/xxxx/.gmt/")  # Need to update this line
    
  6. added
    question
    and removed
    bug
    on Dec 8, 2025
  7. added this to the 0.18.0 milestone on Dec 8, 2025
  8. seisman commented on Dec 8, 2025

    @seisman
    Member

    As explained above, PyGMT can support Chinese characters without issues.

    Ideally, we should be able to provide a gallery example to show PyGMT supports non-standard fonts, as GMT does, but it means we have to offer extra fonts, and also do some font-related configurations, which is not easy to do in our CI.

    So, I'm closing the issue.

  9. CUMT530849827 commented on Dec 8, 2025

    @CUMT530849827

    @seisman Thank you. I successfully solved my problem. I didn't add the gs_option option as required when figur.show (), so the Chinese characters were garbled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions