bar-chart - pygal + 条形图 + 根据 x_label 更改同一系列中值的颜色
问题描述
大家好。
我想使用 pygal 来绘制如上图所示的图像:
根据 x_label 绘制具有不同颜色的相同系列。基本上,我想展示的是工作日和周末之间的区别。
但是,pygal 似乎只允许每个系列的颜色设置。
到目前为止,我能够完成的是以下之一:
1- 将一周中的每一天视为自己的系列,具有自己的颜色,而不显示 x_lables。
代码:
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 中:
代码:
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)
对此有什么想法吗?
谢谢 :)
解决方案
推荐阅读
- android - 对于语言环境“ar”(阿拉伯语),还应定义以下数量:少、多、二、零
- gcloud - Cloud ML Engine 无法在命令行中运行,并说找不到有效的 Python 路径
- reactjs - 尝试上传到 s3 时出现“params.Body is required”错误
- sql - 日期格式和提取日期值
- bash - 在函数中使用变量(传入占位符)
- python - 使用 SGD (loss=hinge) 为 SVM 寻找最相关或最重要的特征
- android - Android aar 在项目布局中找不到 ID
- android - 如何为 Google Home 实现门铃?
- python - 如何在tkinter中为空时禁用输入框?
- c# - 使用 UTF-8 编码的 Restsharp 请求