hoffstadt / DearPyGui

Dear PyGui: A fast and powerful Graphical User Interface Toolkit for Python with minimal dependencies
https://dearpygui.readthedocs.io/en/latest/
MIT License
13.23k stars 688 forks source link

Set plot marker with custom color dict by using add_theme_color #2395

Open tonyzhangnxp opened 1 month ago

tonyzhangnxp commented 1 month ago

Hello there

The API add_theme_color supports color dict as input, but it doesn't work. Here is the code

import random
import dearpygui.dearpygui as dpg
dpg.create_context()

with dpg.theme() as plot_theme:
    with dpg.theme_component(dpg.mvScatterSeries):
        marker_size = dpg.add_theme_style(dpg.mvPlotStyleVar_MarkerSize, 1, category=dpg.mvThemeCat_Plots)
        color_dict = []
        for i in range(10):
            if i %2 == 1:
                color_dict.append((255,0,0,255))
            else:
                color_dict.append((0,255,0,255))
        dpg.add_theme_color(dpg.mvPlotCol_MarkerFill, value=color_dict, category=dpg.mvThemeCat_Plots)

def change_marker_size(_, app_data):
    dpg.set_value(marker_size, [app_data])

with dpg.window():
    dpg.add_slider_int(label="Marker Size", min_value=1, max_value=10, default_value=1, callback=change_marker_size)
    with dpg.plot():
        dpg.add_plot_legend()
        dpg.add_plot_axis(dpg.mvXAxis)
        with dpg.plot_axis(dpg.mvYAxis):

            for _ in range(1):
                x = [random.random() for _ in range(10)]
                y = [random.random() for _ in range(10)]
                dpg.add_scatter_series(x,y)
                dpg.bind_item_theme(dpg.last_item(), plot_theme)
                dpg.add_line_series(x,y)

dpg.create_viewport(width=600, height=500)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

And this is what I got: image

v-ein commented 1 month ago

The API add_theme_color supports color dict as input

Hmm... why do you think it does?

Also, FWIW, color_dict in your code is a list, not a dict.

If you want to draw every marker with a different color, you can render them individually with draw_circle. Maybe there are other ways, it was just first to come to my mind.

tonyzhangnxp commented 1 month ago

Hello @v-ein

Thank you for your quick reply. You are right, color_dict is a list.

I was greatly concerned that the UI would be stuck when using draw_circle to draw a large number of arrays.

BR Tony

v-ein commented 1 month ago

How large is large? How many colors are you going to use, and how many markers?