Python chart does not display values correctly

1

I'm trying to learn how to make graphics in Python. I did one now and it was not very good:

AlldatesareonApril10,2017,onlycomingfrom07:50a.m.to08:40p.m.(GMT-3h)Inthefigure,itappeareduntilthedatetodayand,asaresult,thepointsdisappeared.Onthetimeaxis(Xaxis)wastoappearonlythedateofApril10,2017withthevariationofhour,minuteandsecond!ThisistheproblemHowtofix?

importmatplotlib.pyplotaspltimportmatplotlib.datesasdatesfromdatetimeimportdatetime,timedeltax=[]y=[]dataset=open("dataset_semAtaques.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("Gráfico do número de Conexões por segundo antes do Ataque")
    plt.ylabel("Numero de conexões por segundo")
    plt.xlabel('Tempo')
    plt.gcf().autofmt_xdate()
    plt.show()

The dataset_semAccounts.csv file looks like this:

1491821400,0
1491821580,0
1491821760,0
1491821940,0
1491822120,3
1491822300,3
1491822480,2
1491822660,2
1496012764,3
1491823020,2
1491823200,2
1491823380,2
1491823560,3
1491823740,2
1491823920,2
1491824100,2
1491824280,3
1491824400,2
    
asked by anonymous 29.05.2017 / 01:29

2 answers

7

Simply define the limits of the x-axis using the function set_xlim , as in the example below:

# . . .
plt.title("Grafico do numero de Conexoes por segundo antes do Ataque")
plt.ylabel("Numero de conexoes por segundo")
plt.xlabel('Tempo')

# Linha adicionada:
plt.gca().set_xlim([datetime(2017, 4, 10, 0, 0, 0), datetime(2017, 4, 10, 23, 59, 59)])

plt.gcf().autofmt_xdate()
plt.show()

    
29.05.2017 / 05:12
5
  

In the figure, it appeared until the date of today and, as a result, the points are gone.

They have not disappeared. Try enlarging the image. They are on the edges.

I converted one of your dates to the suggested format and note that there is no millisecond information in it:

>>> datetime.datetime.fromtimestamp(1491821400).strftime('%m/%d/%Y %H:%M:%S.%f')
'04/10/2017 05:50:00.000000'

In CSV generation, check the date format. It may not be enough for your chart.

    
29.05.2017 / 18:21