Cambiare colore barre

di il
5 risposte

Cambiare colore barre

Buon giorno a tutti,

qualcuno ha la vaga idea di come cambiare il colore di ogni singola barra ottenuta tramite il comando PARETO (https://it.mathworks.com/help/matlab/ref/pareto.htm) ???

Grazie a tutti

5 Risposte

  • Re: Cambiare colore barre

    Le barre vengono disegnate come "patch".

    Dato il grafico, puoi usare la funzione "findobj" per ottenere l'handle delle patches:
    
    % Get the handle of the patches
    p=findobj('type','patch')
    
    A questo punto, tramite l'handle puoi ottenere gli indici delle patches ed i valori dei relativi vertici:
    
    % Get the indices of the faces of the patches
    f=get(p,'faces')
    % Get the values of the vertices of the patches
    v=get(p,'vertices')
    
    dal momento che ogni patch è un rettangolo, ognuna avrà 4 vertici; puoi quindi calcolare quante patches ci sono nel grafico:
    
    % Compute the number of patches
    n_patch=size(v,1)/4
    
    A questo punto non ti resta che ri-plottare le patches una ad una specificando il colore (nell'esempio, un colore random)
    
    hold on
    % Re-plot the patches
    for i=1:n_patch
       % Define a random color
       c_set=rand(1,3)
       % Plot the i-th patch
       np=patch('faces',[1:4],'vertices',v(f(i,:),:),'facecolor',c_set)
    end
    
    Ultimo passo, cancellare le patches originali:
    
    % Delete the original pathces
    delete(p)
    
    Di seguito il codice completo.
    ATTENZIONE: testato solo con Octave, potrebbe darsi che ci sia qualche incongruenza con MatLab, ma l'approccio dovrebbe essere corretto.
    
    % Plot a pareto chart
    Cheese = {"Cheddar", "Swiss", "Camembert", ...
                        "Munster", "Stilton", "Blue"};
    Sold = [105, 30, 70, 10, 15, 20];
    p=pareto (Sold, Cheese);
    % Get the handle of the patches
    p=findobj('type','patch')
    % Get the indices of the faces of the patches
    f=get(p,'faces')
    % Get the values of the vertices of the patches
    v=get(p,'vertices')
    % Compute the number of patches
    n_patch=size(v,1)/4
    % Re-plot the patches
    hold on
    for i=1:n_patch
       % Define a random color
       c_set=rand(1,3)
       % Plot the i-th patch
       np=patch('faces',[1:4],'vertices',v(f(i,:),:),'facecolor',c_set)
    end
    % Delete the original pathces
    delete(p)
    

    pareto.JPG
    pareto.JPG

  • Re: Cambiare colore barre

    Ma se volessi plottare un determinato colore per ogni barra ?
  • Re: Cambiare colore barre

    ask_raf ha scritto:


    Di seguito il codice completo.
    ATTENZIONE: testato solo con Octave, potrebbe darsi che ci sia qualche incongruenza con MatLab, ma l'approccio dovrebbe essere corretto.
    
    % Plot a pareto chart
    Cheese = {"Cheddar", "Swiss", "Camembert", ...
                        "Munster", "Stilton", "Blue"};
    Sold = [105, 30, 70, 10, 15, 20];
    p=pareto (Sold, Cheese);
    % Get the handle of the patches
    p=findobj('type','patch')
    % Get the indices of the faces of the patches
    f=get(p,'faces')
    % Get the values of the vertices of the patches
    v=get(p,'vertices')
    % Compute the number of patches
    n_patch=size(v,1)/4
    % Re-plot the patches
    hold on
    for i=1:n_patch
       % Define a random color
       c_set=rand(1,3)
       % Plot the i-th patch
       np=patch('faces',[1:4],'vertices',v(f(i,:),:),'facecolor',c_set)
    end
    % Delete the original pathces
    delete(p)
    

    pareto.JPG

    Lanciando il codice mi plotta il grafico ma le barre sono tutte d i colore blu
  • Re: Cambiare colore barre

    Purtroppo l'assunzione che Octave e MatLab usassero lo stesso approccio per plottare le barre del grafico "pareto" si è rivelata sbagliata:
    [*] Octave usa la funzione patch
    [*] Matlab usa la funzione bar

    Solo a partire dalla versione R2017b è possibile modificare direttamente i colori delle barre; in questo articolo trovi i dettagli mentre la documentazione della funzione bar descrive come modificare i colori.

    L'articolo spiega anche come modificare i colori nel caso si abbia accesso una versione di MatLab precedente alla 2017.

    Di seguito una possibile implementazione della soluzione proposta nell'articolo, i commenti nel codice dovrebbero spiegare i vari passaggi.
    I dati delle barre vengono estratti dai valori della curva presente nel grafico pareto originale.
    
    % Plot a pareto chart
    Cheese = {'Cheddar', 'Swiss', 'Camembert', ...
                        'Munster', 'Stilton', 'Blue'};
    Sold = [105, 30, 70, 10, 15, 20];
    [p,ax]=pareto (Sold,Cheese);
    % Get the index of handle of the pareto bars
    for i=1:length(ax(1).Children)
       if(strcmp(ax(1).Children(i).Type,'bar'))
          bh=i
       end
    end
    % Delete the original pareto bars
    delete(ax(1).Children(bh))
    % Get the pareto data
    pareto_data=[105 diff(ax(1).Children(1).YData(1:end-1))];
    n_data=length(pareto_data);
    % Define the colors of the bars
    bar_c='krgbc';
    hold on
    % Add the bars and set their colour
    for i=1:n_data
       b=bar(i,pareto_data(i))
       b.FaceColor=bar_c(i)
    end
    
    Questo è il risultato che si ottiene:
    Allegati:
    16082_95df8082c4dccb5e4a3ebd1a8c555bf1.jpg
    16082_95df8082c4dccb5e4a3ebd1a8c555bf1.jpg
  • Re: Cambiare colore barre

    Grazie, ho già visto quel link ed era quello che mi serviva!
Devi accedere o registrarti per scrivere nel forum
5 risposte