Chaco 堆叠条形图,每个条形和段的颜色不同
Chaco stacked bar plot with different colour for each bar and segment
我正在尝试控制 Chaco 堆积条形图中每个条形 和 每个条形段的颜色。
例如,运行 Chaco 的堆积条形图示例给出,
我怎样才能把它变成这个怪物?
我可以看到 Chaco 有 ColourMapped Scatter Plot 可以控制每个点。 我需要这种行为,但需要条形图。有没有快速的方法?
这是演示。生成默认 Chaco 堆积条形图的代码
"""
Simple example of a stacked bar chart
"""
# Major library imports
import numpy
# Enthought library imports
from enable.api import ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import UItem, View
# Chaco imports
from chaco.api import LabelAxis, Plot, ArrayPlotData, ArrayDataSource
class PlotExample(HasTraits):
plot = Instance(Plot)
def _plot_default(self):
index = numpy.array([1, 2, 3, 4, 5])
series_a, series_b, series_c = (index * 10, index * 5, index * 2)
# Stack them up
series_c = series_c + series_b + series_a
series_b = series_b + series_a
plot_data = ArrayPlotData(index=index)
plot_data.set_data("series_a", series_a)
plot_data.set_data("series_b", series_b)
plot_data.set_data("series_c", series_c)
plot = Plot(plot_data)
plot.plot(("index", "series_a"), type="bar", bar_width=0.8, color="auto")
plot.plot(
("index", "series_b"),
type="bar",
bar_width=0.8,
color="auto",
starting_value=ArrayDataSource(series_a),
)
plot.plot(
("index", "series_c"),
type="bar",
bar_width=0.8,
color="auto",
starting_value=ArrayDataSource(series_b),
)
# set the plot's value range to 0, otherwise it may pad too much
plot.value_range.low = 0
# replace the index values with some nicer labels
label_axis = LabelAxis(
plot,
orientation="bottom",
title="Months",
positions=list(range(1, 10)),
labels=["jan", "feb", "march", "april", "may"],
small_haxis_style=True,
)
plot.underlays.remove(plot.index_axis)
plot.index_axis = label_axis
plot.underlays.append(label_axis)
return plot
traits_view = View(
UItem("plot", editor=ComponentEditor()), width=400, height=400, resizable=True,
)
demo = PlotExample()
if __name__ == "__main__":
demo.configure_traits()
我会通过为堆叠条形图中的每个部分制作一个单独的条形图渲染器来解决这个问题。像这样的事情应该可以解决问题(对于非常冗长的 for
声明感到抱歉):
"""
Simple example of a stacked bar chart
"""
# Major library imports
import numpy
# Enthought library imports
from enable.api import ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import UItem, View
# Chaco imports
from chaco.api import LabelAxis, Plot, ArrayPlotData, ArrayDataSource
class PlotExample(HasTraits):
plot = Instance(Plot)
def _plot_default(self):
index = numpy.array([1, 2, 3, 4, 5])
series_a, series_b, series_c = (index * 10, index * 5, index * 2)
# Stack them up
series_c = series_c + series_b + series_a
series_b = series_b + series_a
plot_data = ArrayPlotData()
for i, (index_val, series_a_val, series_b_val, series_c_val) in enumerate(zip(
index, series_a, series_b, series_c
)):
plot_data.set_data(f"index_{i}", [index_val])
plot_data.set_data(f"series_a_{i}", [series_a_val])
plot_data.set_data(f"series_b_{i}", [series_b_val])
plot_data.set_data(f"series_c_{i}", [series_c_val])
plot = Plot(plot_data)
for i, (index_val, series_a_val, series_b_val, series_c_val) in enumerate(zip(
index, series_a, series_b, series_c
)):
plot.plot(
(f"index_{i}", f"series_a_{i}"),
type="bar",
bar_width=0.8,
color="auto",
)
plot.plot(
(f"index_{i}", f"series_b_{i}"),
type="bar",
bar_width=0.8,
color="auto",
starting_value=ArrayDataSource([series_a_val]),
)
plot.plot(
(f"index_{i}", f"series_c_{i}"),
type="bar",
bar_width=0.8,
color="auto",
starting_value=ArrayDataSource([series_b_val]),
)
# set the plot's value range to 0, otherwise it may pad too much
plot.value_range.low = 0
# replace the index values with some nicer labels
label_axis = LabelAxis(
plot,
orientation="bottom",
title="Months",
positions=list(range(1, 10)),
labels=["jan", "feb", "march", "april", "may"],
small_haxis_style=True,
)
plot.underlays.remove(plot.index_axis)
plot.index_axis = label_axis
plot.underlays.append(label_axis)
return plot
traits_view = View(
UItem("plot", editor=ComponentEditor()), width=400, height=400, resizable=True,
)
demo = PlotExample()
if __name__ == "__main__":
demo.configure_traits()
截图:
我正在尝试控制 Chaco 堆积条形图中每个条形 和 每个条形段的颜色。
例如,运行 Chaco 的堆积条形图示例给出,
我怎样才能把它变成这个怪物?
我可以看到 Chaco 有 ColourMapped Scatter Plot 可以控制每个点。 我需要这种行为,但需要条形图。有没有快速的方法?
这是演示。生成默认 Chaco 堆积条形图的代码
"""
Simple example of a stacked bar chart
"""
# Major library imports
import numpy
# Enthought library imports
from enable.api import ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import UItem, View
# Chaco imports
from chaco.api import LabelAxis, Plot, ArrayPlotData, ArrayDataSource
class PlotExample(HasTraits):
plot = Instance(Plot)
def _plot_default(self):
index = numpy.array([1, 2, 3, 4, 5])
series_a, series_b, series_c = (index * 10, index * 5, index * 2)
# Stack them up
series_c = series_c + series_b + series_a
series_b = series_b + series_a
plot_data = ArrayPlotData(index=index)
plot_data.set_data("series_a", series_a)
plot_data.set_data("series_b", series_b)
plot_data.set_data("series_c", series_c)
plot = Plot(plot_data)
plot.plot(("index", "series_a"), type="bar", bar_width=0.8, color="auto")
plot.plot(
("index", "series_b"),
type="bar",
bar_width=0.8,
color="auto",
starting_value=ArrayDataSource(series_a),
)
plot.plot(
("index", "series_c"),
type="bar",
bar_width=0.8,
color="auto",
starting_value=ArrayDataSource(series_b),
)
# set the plot's value range to 0, otherwise it may pad too much
plot.value_range.low = 0
# replace the index values with some nicer labels
label_axis = LabelAxis(
plot,
orientation="bottom",
title="Months",
positions=list(range(1, 10)),
labels=["jan", "feb", "march", "april", "may"],
small_haxis_style=True,
)
plot.underlays.remove(plot.index_axis)
plot.index_axis = label_axis
plot.underlays.append(label_axis)
return plot
traits_view = View(
UItem("plot", editor=ComponentEditor()), width=400, height=400, resizable=True,
)
demo = PlotExample()
if __name__ == "__main__":
demo.configure_traits()
我会通过为堆叠条形图中的每个部分制作一个单独的条形图渲染器来解决这个问题。像这样的事情应该可以解决问题(对于非常冗长的 for
声明感到抱歉):
"""
Simple example of a stacked bar chart
"""
# Major library imports
import numpy
# Enthought library imports
from enable.api import ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import UItem, View
# Chaco imports
from chaco.api import LabelAxis, Plot, ArrayPlotData, ArrayDataSource
class PlotExample(HasTraits):
plot = Instance(Plot)
def _plot_default(self):
index = numpy.array([1, 2, 3, 4, 5])
series_a, series_b, series_c = (index * 10, index * 5, index * 2)
# Stack them up
series_c = series_c + series_b + series_a
series_b = series_b + series_a
plot_data = ArrayPlotData()
for i, (index_val, series_a_val, series_b_val, series_c_val) in enumerate(zip(
index, series_a, series_b, series_c
)):
plot_data.set_data(f"index_{i}", [index_val])
plot_data.set_data(f"series_a_{i}", [series_a_val])
plot_data.set_data(f"series_b_{i}", [series_b_val])
plot_data.set_data(f"series_c_{i}", [series_c_val])
plot = Plot(plot_data)
for i, (index_val, series_a_val, series_b_val, series_c_val) in enumerate(zip(
index, series_a, series_b, series_c
)):
plot.plot(
(f"index_{i}", f"series_a_{i}"),
type="bar",
bar_width=0.8,
color="auto",
)
plot.plot(
(f"index_{i}", f"series_b_{i}"),
type="bar",
bar_width=0.8,
color="auto",
starting_value=ArrayDataSource([series_a_val]),
)
plot.plot(
(f"index_{i}", f"series_c_{i}"),
type="bar",
bar_width=0.8,
color="auto",
starting_value=ArrayDataSource([series_b_val]),
)
# set the plot's value range to 0, otherwise it may pad too much
plot.value_range.low = 0
# replace the index values with some nicer labels
label_axis = LabelAxis(
plot,
orientation="bottom",
title="Months",
positions=list(range(1, 10)),
labels=["jan", "feb", "march", "april", "may"],
small_haxis_style=True,
)
plot.underlays.remove(plot.index_axis)
plot.index_axis = label_axis
plot.underlays.append(label_axis)
return plot
traits_view = View(
UItem("plot", editor=ComponentEditor()), width=400, height=400, resizable=True,
)
demo = PlotExample()
if __name__ == "__main__":
demo.configure_traits()
截图: