Python Forum
Printing x values from an csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing x values from an csv file
#1
I am using a .csv file with measurements. On the first column of the .csv file there are timestamps, such as this:
01/24/2021 12:45 PM
(after 100 measurements)
02/25/2021 04:34 PM
. . .
etc.

On the 2nd column there are some numbers. When I plot the data I need to put x values something. Can it put automatically the date time for every lets say 100 measurements, on x axis? Is there any better way? From here: https://matplotlib.org/3.3.3/api/_as_gen....plot.html
I tried:

plot('date', 'visitors', data=obj)
but does not seem to work...on x values it prints me numbers from 0 to 2000, with step 200.
Reply
#2
How can I print the data of a .csv in a period every 50 rows let's say?? Meaning print line 1, print line 51, print line 101, etc??
Reply
#3
>>> group_by = 50
>>> line_num = -1 - group_by
>>> with open('the-file.csv') as f:
...   for line in f:
...     line_num += 1
...     if line_num % group_by == 0:
...       print(f"{line_num}: {line}")
Maybe not the best way.
hobbyist likes this post
Reply
#4
How can I do the same on a plot reading an excel? I mean to plot the graph for all x values, but on the x-axis to put x values every 50 measurements, which will be timestamps from the 1st column of the excel file. I do not know how else to describe it ... For example suppose I have 1500 x-values and I print the graph for all this values (X-axis, Y-axis) but down on the x-axis to print the x-values labels once every 100 values, so totally 15 values on x-axis, meaning:

timestamp_0 that stands for 0,
timestamp_1 that stands for x = 100th value,
timestamps_2 that stands for x = 200th value,
....
timestamp_15 that stands for x = 1500th value

Thank you...
Reply
#5
Could you share what you have now, along with a (small) sample file we can test out with?
Reply
#6
Ok, I need to do something like this, without the need to insert in the code the values of the excel. Just to read the excel and plot the data on x-axis, y-axis every 50 measurements...Any idea? The code is inside the link...
https://stackoverflow.com/questions/6424...s-on-chart

from pandas import Timestamp
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt


df = pd.DataFrame(
                 {'date':
                         {0: Timestamp('2019-10-31 00:00:00'),
                          1: Timestamp('2019-11-30 00:00:00'),
                          2: Timestamp('2019-12-31 00:00:00'),
                          3: Timestamp('2020-01-31 00:00:00'),
                          4: Timestamp('2020-02-29 00:00:00'),
                          5: Timestamp('2020-03-31 00:00:00'),
                          6: Timestamp('2020-04-30 00:00:00'),
                          7: Timestamp('2020-05-31 00:00:00'),
                          8: Timestamp('2020-06-30 00:00:00'),
                          9: Timestamp('2020-07-31 00:00:00'),
                          10: Timestamp('2020-08-31 00:00:00')},
                 'rate':
                         {0: 100.0,
                          1: 99.04595078851037,
                          2: 101.09797599729458,
                          3: 102.29581878702609,
                          4: 104.72409825791058,
                          5: 109.45297539163114,
                          6: 118.24943699089361,
                          7: 119.65432196709045,
                          8: 117.82108184647535,
                          9: 118.6223497519237,
                          10: 120.32838345607335}})


df['datelabel'] = df['date'].apply(lambda x: x.strftime('%b %d'))
chart = sns.lineplot('date', 'rate', data=df,marker="o")
chart.set_xticklabels(df.datelabel, rotation=45)
plt.show()
Reply
#7
The dataset, that I use, is similar the above and is consisted of 10000 rows of those measurements. The guy in the post above inserts the data inside the code. I need to do the same as the guy in the post BUT I want the process of reading the data and putting the xticks/xlabels to be automatically. So for instance read the excel file and put xticks only every 1000 measurements/rows on x-axis. This job should be automatic instead of inserting the data to the code. How can I achieve this?
Reply
#8
In this code:

import pandas as pd
import matplotlib.pyplot as plt

weather_data = pd.read_csv("seattleWeather.csv")

fig = plt.figure()
plt.plot(weather_data['DATE'], weather_data['PRCP'])
fig.autofmt_xdate()
plt.show()
(source: https://stackabuse.com/rotate-axis-label...atplotlib/) it prints all dates in xticks. How can I modify it to print the dates every 10 dates for example??
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing specific values out from a dictionary mcoliver88 6 1,317 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,579 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Overwrite values in XML file with values from another XML file Paqqno 5 3,212 Apr-01-2022, 11:33 PM
Last Post: Larz60+
  How to split file by same values from column from imported CSV file? Paqqno 5 2,705 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  Problem with sum of values from .txt file PathhK 2 2,528 Jan-07-2019, 07:40 PM
Last Post: nilamo
  Printing lines in a basic text file Drone4four 6 3,808 Aug-16-2018, 03:10 PM
Last Post: snippsat
  Printing from a text file not working as I thought it would PythonZenon 10 5,969 Jun-02-2018, 09:19 PM
Last Post: snippsat
  printing a text file by python and thermal printer gray 6 20,420 Jul-30-2017, 05:56 AM
Last Post: buran
  trying to get the keystrokes values to file rwahdan 2 3,923 Jul-14-2017, 06:53 PM
Last Post: rwahdan
  getting current filename of a text editor file for printing file name? hsunteik 3 4,998 Dec-24-2016, 07:58 PM
Last Post: Blue Dog

Forum Jump:

User Panel Messages

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