How to improve a bar graph whose values are very close in Python?

Asked

Viewed 1,394 times

0

I am drawing the bar graph using the following Python code:

# -*- coding: utf-8 -*-

    import matplotlib.pyplot as plt
    import matplotlib.dates as dates
    from datetime import datetime, timedelta

    x = []
    y = []
    with open("Tempo_entropia_IP_dst_Ajustado.csv") as f:
        for l in f:
            X,Y = l.split(",") #separador eh a virgula
            x.append(float(X))
            y.append(float (Y))

    #x1 = [datetime.fromtimestamp(int(d)) for d in x]
    x1 = [str(datetime.fromtimestamp(int(d)))[-8:] for d in x]

    y_pos = [idx for idx, i in enumerate(y)]

    plt.figure(figsize=(17,9))
    plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%m/%d/%Y %H:%M:%S'))

    plt.bar(y_pos, y, align='edge', color="blue", alpha=0.5, width=0.5) # <--- EDICAO PRINCIPAL
    plt.title("Valores X ")tempo
    plt.ylabel("Valores")
    plt.xlabel('Tempo')
    plt.xticks(y_pos, x1, size='small',rotation=35, ha="right")
    plt.yticks(y)
    plt.ylim(ymax=sorted(y)[-1]+1) # valor maximo do eixo y
    #plt.ylim(ymin=sorted(y)[0]-1) # valor minimo do eixo y

    plt.show()

The result was as follows:

Grafico com valores "borrados"

The values I’m using (the first column is Unix time):

1491821400,2.30843204743
1491821460,2.1812196614
1491821520,2.20829106219
1491821580,2.16851682086
1491821640,2.24040710954
1491821700,2.30911323629
1491821760,2.38231474158
1491821820,2.32810974099
1491821880,2.65243973458
1491821940,2.55147322645
1491822000,2.28179058941
1491822060,2.22498572466
1491822120,2.4649167119
1491822180,2.32884884929
1491822240,2.76436949341
1491822300,2.75970251181
1491822360,2.62167716643
1491822420,2.75802496925
1491822480,3.04670231131
1491822540,2.90915626608
1491822600,2.8804431381
1491822660,2.68756157896
1491822720,2.65950418795
1491822780,2.25444575019
1491822840,2.97683607485
1491822900,2.46651363056
1491822960,2.29780701694
1491823020,2.5535794361
1491823080,2.19018891583
1491823140,2.5381032887
1491823200,2.61447170143
1491823260,2.51794893679
1491823320,2.67355730434
1491823380,2.35525098354
1491823440,2.49279610213
1491823500,2.49635795432
1491823560,2.45656941049
1491823620,2.26140360304
1491823680,2.32835892171
1491823740,2.4051725557
1491823800,2.65191888588
1491823860,2.21420965042
1491823920,2.49473287117
1491823980,3.16042960319
1491824040,2.33853063882
1491824100,2.66672827493
1491824160,2.38067622647
1491824220,2.40465932382
1491824280,2.48435819721
1491824400,3.07134742734
1491824460,2.89368402274
1491824520,2.46673959712
1491824580,2.26153085559
1491824640,2.35392678079
1491824700,2.37732058019
1491824760,2.40144686631
1491824820,2.27443348014
1491824880,2.59585955307
1491824940,2.67417033308

Any suggestions on how to improve the chart display? After all the values are very close. I would like to display as many values as possible.

  • 1

    Play random colors in each column, or create a rule by the value ratio, 20% red, 40% yellow, etc..

  • @Anthraxisbr: thanks. Could you, if possible, help modify the code? I get the idea but can’t implement it.

  • 1

    Yes, I can, if Guto’s answer doesn’t solve it, I’ll prepare one at the end of the afternoon.

  • @Anthraxisbr: thanks. I really couldn’t modify!

  • @Anthraxisbr: If you can also post an answer, it will enrich the topic! You could?

1 answer

4


Besides the suggestion already given in the comment (change the color), it is worth changing the xticks and mainly yticks for fewer values. Do not use all values as this decreases the ability to read (for the x-axis) or simply the text is superimposed (y-axis).

If you leave on automatic in general gets good, if not the case, a look at the parameters. For the x-axis, you can use 1 in 4. For the y-axis, you can use the maximum and see how many values (linearly spaced) you need. I recommend having values throughout the y-axis, so the graph is more presentable (which is what you asked for).

Since I already had the code to test, I took it and used the suggestion and put the colormap cool. Here has a list the others that are interesting.

Follows the code:

import matplotlib.pyplot as plt
import matplotlib.dates as dates
from datetime import datetime, timedelta

import numpy as np 

x = []
y = []
with open("teIP.csv") as f:
        for l in f:
            X,Y = l.split(",") #separador eh a virgula
            x.append(float(X))
            y.append(float (Y))


x1 = [str(datetime.fromtimestamp(int(d)))[-8:] for d in x]

y_pos = [idx for idx, i in enumerate(y)]

plt.figure(figsize=(17,9))
plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%m/%d/%Y %H:%M:%S'))

#define of colormap usado
colorlist=np.column_stack(np.array(y)) #lista os valores
colorlist=(colorlist-np.min(colorlist)) 
colorlist=colorlist/np.max(colorlist) 
colorlist=[plt.cm.cool(aa) for aa in colorlist]

for a in y_pos:
  plt.bar(y_pos[a], y[a], align='edge', color=colorlist[0][a], alpha=0.5, width=0.5) 


plt.xticks(y_pos[::4], x1[::4], size='small',rotation=35, ha="right")

plt.yticks(np.arange(0,max(y),0.3))

plt.xlim(xmin=-1)
plt.ylim(ymax=sorted(y)[-1]+0.1) # valor maximo do eixo y

plt.show() 

With this result Graph_1

  • thank you. if you can put the code I thank you.

  • thanks. No problem. No way I’m not charging no. I’m very happy for you to help me. Thank you so much!

  • No stress. And alias, as I do not know if you are warned of the issue, is there :D.

  • Thank you! I saw it now. I’ll test it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.