Python pyplot hist()为每个叠层构件单独设置线型
我试图制作一个堆叠的直方图,其中每个堆叠的组件都有不同的线型和颜色。Python pyplot hist()为每个叠层构件单独设置线型,python,matplotlib,Python,Matplotlib,我试图制作一个堆叠的直方图,其中每个堆叠的组件都有不同的线型和颜色。color参数接受带有每个组件颜色的列表。但是linestyle参数ls没有。是否有方法分别控制每个堆叠组件的面和线样式 import numpy as np import matplotlib.pyplot as plt x = np.random.random(100) # this works: plt.hist([x,x], histtype='stepfilled', stacked=True, color=['r
color
参数接受带有每个组件颜色的列表。但是linestyle参数ls没有。是否有方法分别控制每个堆叠组件的面和线样式
import numpy as np
import matplotlib.pyplot as plt
x = np.random.random(100)
# this works:
plt.hist([x,x], histtype='stepfilled', stacked=True, color=['r', 'b'])
# this does not:
plt.hist([x,x], histtype='stepfilled', stacked=True, color=['r', 'b'], ls=['-', '--'])
理想情况下,我希望完全控制每个堆叠组件的面颜色和边颜色的颜色和alpha
。可能吗?我尝试过使用fc
选项,但也不接受列表
我还看了这个例子[1],它显示了ax2上的这种行为,但它看起来非常黑客化。例如,以下代码产生错误结果:
plt.hist([(0,1,1), (0,0,1)], histtype='step', stacked=True, fill=True)
有一种解决方法,可以捕获补丁
并单独控制它们,如下面的回答[2]。但是,我想知道是否也可以直接从plt.hist()执行此操作
[1]
[2] 您可以使用与循环其他参数相同的方法
import numpy as np
import matplotlib.pyplot as plt
from cycler import cycler
plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b']) +
cycler('linestyle', ['-', '--', ':']) +
cycler('linewidth', [4,3,1])) )
x = np.random.random(100)
plt.hist([x,x*0.8,x*0.5], histtype='stepfilled', stacked=True )
plt.show()
旁注:我目前不知道为什么在这个例子中循环顺序是颠倒的,即蓝色形状有一个粗实线样式。但我想可以根据需要进行调整。谢谢!但我想这会改变所有后续绘图的属性循环
?因此,在绘制hist
后,必须重置它。我想我还是会对一种更“直接”的方式感兴趣。