Python chart does not display all desired values

Asked

Viewed 968 times

1

I would like only the values of the x, y coordinates corresponding to the points to appear in the graph. But it’s not like this:

Gráfico incorreto

For example:

The point whose x coordinate is 04/10/2017 09:41:00 does not display the value of its y coordinate.

Next point does not display the value of coordinate X.

The Python code is as follows:

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


x = []
y = []

dataset = open("/datasetDdos10Abril2017_unixtime_slowloris.csv","r")
##separacao no csv eh por virgulas


for line in dataset:
    line = line.strip() #23,24\n -> 23,24 retira a quebra de linha
    X,Y = line.split(",") #separador eh a virgula
    x.append( float(X))
    y.append(float (Y))

dataset.close()


x1 = [datetime.fromtimestamp(int(d)) for d in x]


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

plt.plot(x1, y, 'ro')

plt.title("Número de Conexões por segundo: Ataque Sockstress")
plt.ylabel("Número de Conexões por segundo")
plt.xlabel('Tempo')

#plt.gca().set_ylim([0, 29800])
plt.gcf().autofmt_xdate()
plt.show()

The datasetDdos10Abril2017_unixtime_slowloris.csv file looks like this:

1491828000 ,1959
1491828060 ,1652
1491828120 ,1673
1491828180,1610
1491828240,1620
1491828300,3252
1491828360,1617
1491828420 ,1658
1491828480 ,1605
1491828540 ,1615
1491828600 ,1613
1491828660 ,1626
1491828720 ,1640
1491828780 ,1638
1491828840 ,1625
1491828900 ,1580
1491828960 ,1618
1491829020 ,1608
1491829080 ,1619
1491829140 ,1628
1491829200 ,1626
1491829260 ,1640
1491829320 ,1639
1491829380 ,1631
1491829440 ,1635
1491829500 ,1646
1491829560 ,1633
1491829620 ,1599
1491829680 ,1660
1491829740 ,1655
1491829800 ,1647
1491829860 ,1646
1491829920 ,1651
1491829980 ,1625
1491830040 ,1546
1491830100 ,1580
1491830160 ,1614
1491830220,1631
1491830280 ,1618
1491830340 ,1633

Where the first column is Unix times and the second column represents the number of connections per second.

How to correct the graph to display the coordinate values of ALL points (x,y)

  • @Miguel: You can help?

1 answer

2


Solution for the shaft x:

xticks defines the location of ticks and of Labels on the axis x, then just pass x1 for him:

plt.xticks(x1)

Solution for the shaft y:

To the y, the numbers were very close and therefore unreadable - to eliminate some, as suggested, we can do so:

y1 = []
v = 0
for i in sorted(y):
    if(abs(i-v > 50)): # 50 é a distância mínima entre cada número
        y1.append(i)
        v = i

And we put it in the same way:

plt.yticks(y1) # usamos yticks, porque agora é no eixo y

Upshot:

plot com posição correta sobre timestamp

Complete code:

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

x = []
y = []

dataset = open("./datasetDdos10Abril2017_unixtime_slowloris.csv","r")
##separacao no csv eh por virgulas

for line in dataset:
    line = line.strip() #23,24\n -> 23,24 retira a quebra de linha
    X,Y = line.split(",") #separador eh a virgula
    x.append( float(X))
    y.append(float (Y))

dataset.close()

x1 = [datetime.fromtimestamp(int(d)) for d in x]

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

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

plt.plot(x1, y, 'ro')

plt.title("Número de Conexões por segundo: Ataque Sockstress")
plt.ylabel("Número de Conexões por segundo")
plt.xlabel('Tempo')
plt.xticks(x1)
plt.yticks(y1)

#plt.gca().set_ylim([0, 29800])
plt.gcf().autofmt_xdate()
plt.show()
  • Thanks! And the y? axis gets weird?

  • @Eds were only on top of each other because they were very close... :(

  • has how I cut some values? For example, 5100 and 5200 have no points... So would reduce the values!!!!

  • @Eds I edited to include a possible solution, take a look!

  • :Could you leave the two answer options? This and your previous one?

  • @Eds yes! in fact I believe that the two are there - anyway I edited to try to make it clearer - to modify only the x, just do not use the code of the solution y, or comment on the line plt.yticks(y1). (i had misspelled the yticks call, fixed that too).

  • :modified the dataset a bit and the code no longer works. Would you have help? If you prefer, I can create a new question!

  • @Eds I advise you to do just that - I believe I’ll be quicker to get an answer! :)

Show 3 more comments

Browser other questions tagged

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