python-pptx 中的颜色图表

问题描述 投票:0回答:3

我需要在 PowerPoint 中制作图表。但我无法让它变得丰富多彩。例如,数字 1 应为红色,数字 2 - 为蓝色,数字 3 - 绿色,具体取决于先前设置的参数。使用Python-pptx。请告诉我如何解决这个问题。

我的代码:

# -*- coding: utf-8 -*-
from pptx import Presentation
from pptx.enum.chart import XL_CHART_TYPE
from pptx.chart.data import ChartData
from pptx.util import Inches
from pptx.enum.chart import XL_TICK_MARK
from pptx.util import Pt
from pptx.dml import line
from pptx.enum.chart import XL_TICK_LABEL_POSITION
from pptx.dml.color import RGBColor
from pptx.enum.chart import XL_LABEL_POSITION

#if a>50: color='green'
#if a>20 and a<50: color='blue'
#if a<20: color='red'
#if b>50: color='green'
#if b>20 and b<50: color='blue'
#if b<20: color='red'
#if c>50: color='green'
#if c>20 and c<50: color='blue'
#if c<20: color='red'

a=70
b=30
c=40


# create presentation with 1 slide ——
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts)
# define chart data ———————
chart_data = ChartData()
chart_data.categories = 
chart_data.add_series('',(a,b,c))

# add chart to slide ——————–
x, y, cx, cy = Inches(1), Inches(2), Inches(3), Inches(3)
graphic_frame = slide.shapes.add_chart(
XL_CHART_TYPE.BAR_CLUSTERED, x, y, cx, cy, chart_data
)
chart = graphic_frame.chart


category_axis = chart.category_axis
category_axis.visible = False
category_axis.has_major_gridlines = False 
category_axis.has_minor_gridlines = False
category_axis.major_tick_mark = XL_TICK_MARK.NONE
category_axis.minor_tick_mark = XL_TICK_MARK.NONE
category_axis.tick_labels.font.size = Pt(12)

value_axis = chart.value_axis
value_axis.minimum_scale = 0
value_axis.maximum_scale = None
value_axis.has_major_gridlines = False 
value_axis.has_minor_gridlines = False
value_axis.minor_tick_mark = XL_TICK_MARK.NONE
value_axis.major_tick_mark = XL_TICK_MARK.NONE
value_axis.axis_labels = False
value_axis.tick_label_position=XL_TICK_LABEL_POSITION.NONE
value_axis.has_axis_labels=False
tick_labels = value_axis.tick_labels
tick_labels.visible=False

plot = chart.plots
plot.has_data_labels = True
data_labels = plot.data_labels

data_labels.font.size = Pt(13)
data_labels.font.color.rgb = RGBColor(0x0A, 0x42, 0x80)
data_labels.position = XL_LABEL_POSITION.OUTSIDE_END
tick_labels.font.color.rgb=RGBColor(0x0A, 0x42, 0x80)

prs.save('chart-02.pptx') 

我使用 Python 2.7,32 位

python python-2.7 charts powerpoint python-pptx
3个回答
4
投票

我知道这是一个老问题,但以防万一有人像我一样在这里绊倒。这就是我为不同系列的图表着色的方式:

colors = {}
colors['seriesa'] = '3C2F80'
colors['seriesb'] = '3C2F81'
colors['seriesc'] = '3C2F82'

for series in chart.series:
    if series.name in colors:
        fill = series.format.fill #fill the legend as well
        fill.solid()
        fill.fore_color.rgb = RGBColor.from_string(colors[series.name])

0
投票

这是一种在 python pptx 中为图表着色的方法:

声明一个类将帮助您为图表分配多种颜色

from pptx.dml.color import RGBColor
class ColorCodes:
    PRIMARY_COLOR = RGBColor(255, 222, 23)
    SECONDARY_COLOR = RBGColor(255,0,0)

    #graphic_frame is the variable where shape is assigned
    chart = graphic_frame.chart
    chart_points = chart.plots[0].series[0].points
    accent_colors = [ColorCodes.PRIMARY_COLOR, ColorCodes.SECONDARY_COLOR]
    for point, accent_color in zip(chart_points, accent_colors):
                fill = point.format.fill
                fill.solid()
                for i in range(len(accent_colors)):
                    fill.fore_color.rgb = accent_colors[i]

如果您想要所有系列的静态颜色,您可以直接声明它

fill.fore_color.rgb = RGBColor(0,255,255)

如果您想要为图表预定义 python pptx 颜色,您可以按照此处提供的解决方案进行操作:Python pptx 为每个类别自定义颜色


0
投票

示例:

from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
from pptx.dml.color import RGBColor

# Create presentation with 1 slide
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])

# Define chart data
chart_data = CategoryChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (19.2, 21.4, 16.7))

# Add chart to slide
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
chart = slide.shapes.add_chart(
    XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
).chart

# Map series names to lists of colors
colors = {
    'Series 1': ['3C2F80', 'FF5733', '42D4F4'],  # Example colors
    # Add more series names and color lists as needed
}

# Change colors of chart series
for series in chart.series:
    if series.name in colors:
        for i, color_code in enumerate(colors[series.name]):
            if i < len(series.points):
                fill = series.points[i].format.fill
                fill.solid()
                fill.fore_color.rgb = RGBColor.from_string(color_code)

# Save presentation
prs.save('chart-01-custom-colors.pptx')
© www.soinside.com 2019 - 2024. All rights reserved.