Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with barPlot
#1
Hello,

I need to plot this bar graph where the values ​​from one bar to the other are very close (we have a difference in the second to third decimal place), so the sizes of my bars are getting practically the same. I needed them to be in different sizes, taking into account this small difference, can anyone help me how to do this?

My code and files are attached.

[Image: 2q4ZvxV]

import matplotlib.pyplot as plt
import numpy as np
import pickle
import plotly.graph_objects as go

with open('cifar100_subset_19_kDN_history_30repeats_random.pkl', 'rb') as file_pi:
    random_history = pickle.load(file_pi)
with open('cifar100_subset_19_kDN_history_30repeats_curriculum.pkl', 'rb') as file_pi:
    curriculum_history = pickle.load(file_pi)
with open('cifar100_subset_19_kDN_history_30repeats_anti.pkl', 'rb') as file_pi:
    anti_history = pickle.load(file_pi)
with open('cifar100_subset_19_kDN_history_30repeats_random.pkl', 'rb') as file_pi:
    curriculum_historySVM = pickle.load(file_pi)
with open('cifar100_subset_19_kDN_history_30repeats_curriculum.pkl', 'rb') as file_pi:
    anti_historySVM = pickle.load(file_pi)
with open('cifar100_subset_19_kDN_history_30repeats_anti.pkl', 'rb') as file_pi:
    random_historySVM = pickle.load(file_pi)

def bar_plot(histories, labels, colors):
    fig = go.Figure()
    trace_list = []

    for history, label, color in zip(histories, labels, colors):

        if 'std_val_acc' in history.keys():
            trace = go.Bar(
                x=[label],
                y=[history['val_acc'][-1]],
                name=label,
                marker_color=color,
                text=['{:.3f} ± {:.3f}'.format(history['val_acc'][-1], history['std_val_acc'][-1])],
                #text=['{:.3f}'.format(history['val_acc'][-1])],
                textposition='outside',
                error_y=dict(
                    type='data', 
                    array=[history['std_val_acc'][-1]],
                    thickness=2,
                    width=20,
                    visible=True),
                showlegend=False #len(histories) > 1
            )
        else:
            trace = go.Bar(
                x=[label],
                y=[history['val_acc'][-1]],
                name=label,
                marker_color=color,
                 #text=['{:.3f} ± {:.3f}'.format(history['val_acc'][-1], history['std_val_acc'][-1])],
                 #text=['{:.3f}'.format(history['val_acc'][-1])],
                 textposition='outside',
                showlegend=False #len(histories) > 1
            )

        trace_list.append(trace)

    fig.add_traces(trace_list)

    # Edição do layout
    fig.update_layout(
        legend=dict(
            font=dict(
                size=14
            )
        )
    )
    
    fig.update_traces(
#         texttemplate='%{text:.2f} %{error}',
        textfont_size=24
    )

    fig.update_xaxes(
            tickangle = 0,
            title_text = "Curriculum",
            title_font = {"size": 20},
            title_standoff = 15,
            tickfont=dict(size=14)
    )
    
    fig.update_yaxes(nticks=15)
    fig.update_yaxes(
            tickangle = 0,
            title_text = "Accuracy",
            title_font = {"size": 20},
            title_standoff = 10
    )
    
    
    #fig['layout']['yaxis'].update(range=[0, 0.9], dtick=0.01, tickformat=".3f")
    #fig['layout']['yaxis'].update(yaxis=dict(tickformat=".3f"))

    return fig

# Exemplo de uso
histories = [random_history,
      curriculum_history,
      anti_history, curriculum_historySVM, anti_historySVM, random_historySVM
]

labels = ['Random', 'Curriculum', 'Anti', 'Curriculum baseline', 'Anti baseline', 'Random baseline']
colors = ['red', 'blue', 'green', 'yellow', 'gray', 'black']
fig = bar_plot(histories, labels, colors)
fig.show()

Attached Files

.zip   graficos2.zip (Size: 162.92 KB / Downloads: 309)
Reply
#2
Scale the values. Find the min and max (numpy min() and max() functions help), subtract min from each value and multiply by a factor to make larger. If you want range of 0-10, values would be (value-min)*10/(max-min)
Reply
#3
(Aug-12-2021, 01:54 PM)jefsummers Wrote: Scale the values. Find the min and max (numpy min() and max() functions help), subtract min from each value and multiply by a factor to make larger. If you want range of 0-10, values would be (value-min)*10/(max-min)

Thank you very much for your help, it worked!!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020