python - 增加 matplotlib 字符串字体大小

标签 python matplotlib styling rc

我希望增加绘图标题的字体大小,例如将其从

我的绘图正在从 matplotlibrc .conf 文件获取基本字体大小和修饰符。 这通常表达如下:

font.size : 8
axes.titlesize : medium

现在,对于我的一个图,我想特别增加标题字体大小。 如果这是一个数字(例如线宽),我可以这样做:

from matplotlib import rcParams
newsize = rcParams['lines.linewidth']*2

但这并不那么容易实现。 当然,我可以使用字典或一系列 elif 语句 - 但我在想 matplotlib,知道如何解释这些字符串,也许还有一些函数来增加它们。你知道这样的事吗?

编辑:有人建议这将与其他问题重复。这个问题特别询问增加“字符串字体大小”(正如我在问题标题中所写的)。这意味着手动将字体大小设置为某个整数不是一个选项(除非有一种方法可以将 matplotlibrc 的 smallmedium 转换为整数 - 这也是唯一的这个问题)。

最佳答案

所以问题似乎是:给定一个“axes.titlesize”rcParam,如何将标题大小设置为下一个更高的相对大小规范。

解决方案肯定取决于“axes.titlesize”中可能存在的值。例如,如果我们已经知道这些值只能是 'xx-small','x-small','small','medium','large','x-large'之一,解决方案相当简单。人们只需在所有项目的列表中获取下一个可能的项目。

import matplotlib.pyplot as plt
sizes = ['xx-small','x-small','small','medium','large','x-large','xx-large']
plt.rcParams["axes.titlesize"] = sizes[sizes.index(plt.rcParams["axes.titlesize"])+1]

一般情况,其中“axes.titlesize”可以是数字或任何有效的大小字符串,将通过以下方式处理:

import matplotlib.pyplot as plt
sizes = ['xx-small','x-small','small','medium','large','x-large','xx-large']
cs = plt.rcParams["axes.titlesize"]
try:
    plt.rcParams["axes.titlesize"] = float(cs)*1.2
except:
    if cs in ["larger", "smaller"]:
        cs = cs[:5]
    if cs in sizes[:-1]:    
        plt.rcParams["axes.titlesize"] = sizes[sizes.index(cs)+1]
    elif cs == 'xx-large':
        plt.rcParams["axes.titlesize"] =  plt.rcParams["font.size"]*1.2**4

关于python - 增加 matplotlib 字符串字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47782185/

相关文章:

python - SQLAlchemy 要求为查询设置别名,但在生成的 SQL 中未使用该别名

python - 复合相等性测试在 python 中短路了吗?

Python Pandas Dataframe - 基于条件的分组和平均

javascript - 将所有图像放入DIV标签中

html - 我如何防止 CSS 样式影响我网站的其他部分?

html - 如何用我自己的下拉列表箭头替换?

python - RDD 沿袭/Spark 操作图的良好输出

matplotlib - 如何在不根据原始数据计算的情况下绘制宽度不等的直方图?

python - 图例在 matplotlib 中使用 PathCollections

python - 在两个 y 轴上设置自定义日期 ytick 标签