更改默认轮廓中负值的破折号样式 - matplotlib
change dash style of negative values in a default contour - matplotlib
我正在尝试用 matplotlib 绘制等高线,我在数据中有负值,我希望它们是虚线(matplotlib 默认情况下这样做)但是,我想 (1) 控制虚线样式(打开, off) 和 (2) 单独改变负轮廓的颜色。我在 link 中尝试了答案:How can I set the dash length in a matplotlib contour plot
但这会将轮廓中的所有线条设置为我不想要的破折号。我需要单独破解负轮廓线样式!
我的部分代码:
from pylab import *
import matplotlib
import numpy as np
matplotlib.rcParams['contour.negative_linestyle']= 'dashed'
CS = ax1.contour(xi, yi, W_t, levels=levels, colors='k', linewidths=0.05)
for c in CS.collections:
c.set_dashes([(0, (2.0, 2.0))])
您可以遍历由 CS
对象创建的线条集合,并且对于任何非实线(来自 get_linetype
,值为 [(None, None)]
),您可以根据需要设置它们。作为一个最小的例子,
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
#Dummy data
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10.0 * (Z2 - Z1)
CS = plt.contour(X, Y, Z, 20, colors='k')
for line in CS.collections:
if line.get_linestyle() == [(None, None)]:
print("Solid Line")
else:
line.set_linestyle([(0, (12.0, 3.0))])
line.set_color('red')
plt.show()
我正在尝试用 matplotlib 绘制等高线,我在数据中有负值,我希望它们是虚线(matplotlib 默认情况下这样做)但是,我想 (1) 控制虚线样式(打开, off) 和 (2) 单独改变负轮廓的颜色。我在 link 中尝试了答案:How can I set the dash length in a matplotlib contour plot
但这会将轮廓中的所有线条设置为我不想要的破折号。我需要单独破解负轮廓线样式!
我的部分代码:
from pylab import *
import matplotlib
import numpy as np
matplotlib.rcParams['contour.negative_linestyle']= 'dashed'
CS = ax1.contour(xi, yi, W_t, levels=levels, colors='k', linewidths=0.05)
for c in CS.collections:
c.set_dashes([(0, (2.0, 2.0))])
您可以遍历由 CS
对象创建的线条集合,并且对于任何非实线(来自 get_linetype
,值为 [(None, None)]
),您可以根据需要设置它们。作为一个最小的例子,
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
#Dummy data
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10.0 * (Z2 - Z1)
CS = plt.contour(X, Y, Z, 20, colors='k')
for line in CS.collections:
if line.get_linestyle() == [(None, None)]:
print("Solid Line")
else:
line.set_linestyle([(0, (12.0, 3.0))])
line.set_color('red')
plt.show()