Python Forum

Full Version: Printing x values from an csv file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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??
>>> 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.
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...
Could you share what you have now, along with a (small) sample file we can test out with?
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()
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?
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??