Bar chart made in Python became "weird". Any suggestions how to improve it?

Asked

Viewed 200 times

1

Friends,

The following chart was generated:

Grafico sem ataques

The code used was the following:

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

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

x = []
y = []
with open("dataset_semAtaques__10Abril2017_1min.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]
y_pos = [idx for idx, i in enumerate(y)]

plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%m/%d/%Y %H:%M:%S'))

y1 = []
v = 0
y_sorted = sorted(y)
for i in y_sorted:
    if(abs(i-v > 50)):
        y1.append(i)
        v = i

plt.bar(y_pos, y, align='edge', color="blue", alpha=0.5, width=0.5) # <--- EDICAO PRINCIPAL

plt.title("Número de Conexões por segundo: Sem Ataques")
plt.ylabel("Número de Conexões por segundo")
plt.xlabel('Tempo')
plt.xticks(y_pos, x1, size='small',rotation=35, ha="right")
plt.yticks(y1)
plt.ylim(ymin=y_sorted[0]-200) # valor minimo do eixo y

plt.show()

The bar chart got very strange.

Any suggestions on how to present the chart better with the data below?

The data file (CSV) is available at: https://ufile.io/h8la7

  • This code with python3.5 on my chart looks like this: https://ibb.co/mUwbnQ

  • @Miguel: you are using the dataset of the other issue. The dataset above are few connections, like: 0, 8,2 ...

  • @Miguel: The new dataset is https://ufile.io/h8la7

1 answer

3


The problem is the gap you had for the axis of y, n-200 as a minimum value, and it would be negative since the spectrum of your values is in the range 0 <= y < 10.

Thus it works well, tested with python3.5:

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

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

x = []
y = []
with open("dataset_semataques__10abril2017_1min.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]
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("Número de Conexões por segundo: Sem Ataques")
plt.ylabel("Número de Conexões por segundo")
plt.xlabel('Tempo')
plt.xticks(y_pos, x1, size='small',rotation=35, ha="right")
plt.yticks(y)
plt.ylim(ymax=max(y)+1) # valor maximo do eixo y
#plt.ylim(ymin=min(y)-1) # valor minimo do eixo y

plt.show()
  • how to display y-axis values? Not shown: https://ufile.io/p7b9g

  • Yes, they appear @Eds, copy the code on top after the last edit

  • gratidao. sorry for the silly questions!

  • No problem, we all have @Eds

Browser other questions tagged

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