首页 > 解决方案 > pygal + 条形图 + 根据 x_label 更改同一系列中值的颜色

问题描述

工作日值

大家好。

我想使用 pygal 来绘制如上图所示的图像:

根据 x_label 绘制具有不同颜色的相同系列。基本上,我想展示的是工作日和周末之间的区别。

但是,pygal 似乎只允许每个系列的颜色设置。

到目前为止,我能够完成的是以下之一:

1- 将一周中的每一天视为自己的系列,具有自己的颜色,而不显示 x_lables。 显示的工作日,没有 x_labels 7 颜色

代码:

import pygal
from pygal.style import Style

y_values = [12.85, 12.78, 13.74, 16.73, 12.52, 3.71, 1.96]
x_labels = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
filename = 'barchar_test.png'


chart_config = {
    "human_readable": True,
    "pretty_print": True,
    "truncate_legend": -1,
    "value_font_size": 15,
    "print_values": True,
    "show_legend": False,
    "print_values_position": "top",
    "print_labels": True,
    "value_formatter": lambda x: "{0: .2f}".format(x),
}
style_config = {
    "font_family": "googlefont:lato",
    "plot_background": "white",
    "value_font_size": 15,
    "show_y_guides": False,
    "show_y_labels": False,
    "colors": ("#0099d6", "#0099d6", "#0099d6", "#0099d6", "#0099d6", "#6d6f71", "#6d6f71"),
}

def _plot_bar_chart(y_values, x_labels, filename):
    bar_chart = pygal.Bar(style=Style(**style_config), **chart_config)
    for i, item in enumerate(y_values):
        bar_chart.add(
            x_labels[i], {item},
        )
    bar_chart.render_to_png(filename)

_plot_bar_chart(y_values, x_labels, filename)

2-将值视为一个系列,显示 x_labels,但仅以一种颜色: 显示工作日,一种颜色 代码:

    dimport pygal
from pygal.style import Style

y_values = [12.85, 12.78, 13.74, 16.73, 12.52, 3.71, 1.96]
x_labels = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
filename = 'barchar_test.png'


chart_config = {
    "human_readable": True,
    "pretty_print": True,
    "truncate_legend": -1,
    "value_font_size": 15,
    "print_values": True,
    "show_legend": False,
    "print_values_position": "top",
    "print_labels": True,
    "value_formatter": lambda x: "{0: .2f}".format(x),
}
style_config = {
    "font_family": "googlefont:lato",
    "plot_background": "white",
    "value_font_size": 15,
    "show_y_guides": False,
    "show_y_labels": False,
    "colors": ("#0099d6", "#0099d6", "#0099d6", "#0099d6", "#0099d6", "#6d6f71", "#6d6f71"),
}

def _plot_bar_chart(y_values, x_labels, filename):
    bar_chart = pygal.Bar(style=Style(**style_config), **chart_config)
    bar_chart.x_labels = x_labels
    bar_chart.add('', y_values)
    bar_chart.render_to_png(filename)

_plot_bar_chart(y_values, x_labels, filename)

3- 将值视为单独的系列,显示 x_labels,但将所有条形图堆叠在第一个 x_label 中: 显示的工作日,x_labels 7 颜色

代码:

import pygal
from pygal.style import Style

y_values = [12.85, 12.78, 13.74, 16.73, 12.52, 3.71, 1.96]
x_labels = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
filename = 'barchar_test.png'


chart_config = {
    "human_readable": True,
    "pretty_print": True,
    "truncate_legend": -1,
    "value_font_size": 15,
    "print_values": True,
    "show_legend": False,
    "print_values_position": "top",
    "print_labels": True,
    "value_formatter": lambda x: "{0: .2f}".format(x),
}
style_config = {
    "font_family": "googlefont:lato",
    "plot_background": "white",
    "value_font_size": 15,
    "show_y_guides": False,
    "show_y_labels": False,
    "colors": ("#0099d6", "#0099d6", "#0099d6", "#0099d6", "#0099d6", "#6d6f71", "#6d6f71"),
}

def _plot_bar_chart(y_values, x_labels, filename):
    bar_chart = pygal.Bar(style=Style(**style_config), **chart_config)
    bar_chart.x_labels = x_labels
    for i, item in enumerate(y_values):
        bar_chart.add(
            x_labels[i], {item},
        )
    bar_chart.render_to_png(filename)

_plot_bar_chart(y_values, x_labels, filename)

对此有什么想法吗?

谢谢 :)

标签: bar-chartpygal

解决方案


推荐阅读