生成内联标签值后是否可以编辑等高线图的内联标签?
Is it possible to edit the inline labels of a contour plot after the inline label values are generated?
下面是生成带有内联标签的等高线图的示例代码。我想知道如何编辑内联标签。
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
def z_func(x, y):
""" z = z(x,y) ==> Z = Z(X, Y) """
X, Y = np.meshgrid(x, y)
Z = np.sqrt(X**2 + Y**2)
return X, Y, Z
def get_xyz_contour_plot(X, Y, Z, cmap='plasma', ncontours=6, linecolor='white'):
""" Generates a filled contour plot with inline labels """
plt.contourf(X, Y, Z, cmap=cmap)
contours = plt.contour(X, Y, Z, ncontours, colors=linecolor)
plt.clabel(contours, inline=True, fontsize=8)
plt.show()
X, Y, Z = z_func(x, y)
get_xyz_contour_plot(X, Y, Z)
上面的代码生成一个 plot that looks like this。如果我想给行内标签添加一个负号,我可以在上面的例子中应用一个负号。但出于我的实际目的,我正在绘制与卡方值相关联的 p 值的等高线图。这里的代码太长 post(因此是上面的替代示例),但我最小化了与卡方相关的负 p 值而不是卡方本身(通过 scipy)。因此,我的函数产生负输出,内联标签显示负号。
是否可以在生成内联标签值后通过删除负号来编辑内联标签?例如,如何在不更改 z_func
?
的情况下向上面代码中的内联标签添加负号
您可以为 clabel
指定一个 fmt
,它可能是一个 matplotlib 格式化程序实例。您可以将 FuncFormatter
与一个函数一起使用,该函数只是在格式化之前反转值的符号。
fmt_func = lambda x,pos: "{:1.3f}".format(-x)
fmt = matplotlib.ticker.FuncFormatter(fmt_func)
plt.clabel(contours, inline=True, fontsize=8, fmt=fmt)
完整示例:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
def z_func(x, y):
""" z = z(x,y) ==> Z = Z(X, Y) """
X, Y = np.meshgrid(x, y)
Z = np.sqrt(X**2 + Y**2)
return X, Y, Z
def get_xyz_contour_plot(X, Y, Z, cmap='plasma', ncontours=6, linecolor='white'):
""" Generates a filled contour plot with inline labels """
plt.contourf(X, Y, Z, cmap=cmap)
contours = plt.contour(X, Y, Z, ncontours, colors=linecolor)
fmt_func = lambda x,pos: "{:1.3f}".format(-x)
fmt = matplotlib.ticker.FuncFormatter(fmt_func)
plt.clabel(contours, inline=True, fontsize=8, fmt=fmt)
plt.show()
X, Y, Z = z_func(x, y)
get_xyz_contour_plot(X, Y, Z)
下面是生成带有内联标签的等高线图的示例代码。我想知道如何编辑内联标签。
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
def z_func(x, y):
""" z = z(x,y) ==> Z = Z(X, Y) """
X, Y = np.meshgrid(x, y)
Z = np.sqrt(X**2 + Y**2)
return X, Y, Z
def get_xyz_contour_plot(X, Y, Z, cmap='plasma', ncontours=6, linecolor='white'):
""" Generates a filled contour plot with inline labels """
plt.contourf(X, Y, Z, cmap=cmap)
contours = plt.contour(X, Y, Z, ncontours, colors=linecolor)
plt.clabel(contours, inline=True, fontsize=8)
plt.show()
X, Y, Z = z_func(x, y)
get_xyz_contour_plot(X, Y, Z)
上面的代码生成一个 plot that looks like this。如果我想给行内标签添加一个负号,我可以在上面的例子中应用一个负号。但出于我的实际目的,我正在绘制与卡方值相关联的 p 值的等高线图。这里的代码太长 post(因此是上面的替代示例),但我最小化了与卡方相关的负 p 值而不是卡方本身(通过 scipy)。因此,我的函数产生负输出,内联标签显示负号。
是否可以在生成内联标签值后通过删除负号来编辑内联标签?例如,如何在不更改 z_func
?
您可以为 clabel
指定一个 fmt
,它可能是一个 matplotlib 格式化程序实例。您可以将 FuncFormatter
与一个函数一起使用,该函数只是在格式化之前反转值的符号。
fmt_func = lambda x,pos: "{:1.3f}".format(-x)
fmt = matplotlib.ticker.FuncFormatter(fmt_func)
plt.clabel(contours, inline=True, fontsize=8, fmt=fmt)
完整示例:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
def z_func(x, y):
""" z = z(x,y) ==> Z = Z(X, Y) """
X, Y = np.meshgrid(x, y)
Z = np.sqrt(X**2 + Y**2)
return X, Y, Z
def get_xyz_contour_plot(X, Y, Z, cmap='plasma', ncontours=6, linecolor='white'):
""" Generates a filled contour plot with inline labels """
plt.contourf(X, Y, Z, cmap=cmap)
contours = plt.contour(X, Y, Z, ncontours, colors=linecolor)
fmt_func = lambda x,pos: "{:1.3f}".format(-x)
fmt = matplotlib.ticker.FuncFormatter(fmt_func)
plt.clabel(contours, inline=True, fontsize=8, fmt=fmt)
plt.show()
X, Y, Z = z_func(x, y)
get_xyz_contour_plot(X, Y, Z)