Python Forum
Print names in x-axis of a time-series values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print names in x-axis of a time-series values
#1
Hello, I have a .csv that has 2 columns. The first column contains timestamps and the second column CO2 measurements. What I want to do is to plot the data, but on x-axis instead of depicting timestamps I want to print strings, such as: "name_1" , "name_2", "name_3"... Any ideas?
Reply
#2
Swapping what columns is x or y is easy.
import pandas as pd
import random
import matplotlib.pyplot as plt


times = pd.date_range(start='2022-03-1', end='2022-03-21', periods=21)
levels = [random.randint(1, 10) for _ in times]

df = pd.DataFrame({"date": times, "level": levels})
print(df)

df.plot(kind="line", x="date", y="level")
df.plot(kind="scatter", x="level", y="date")

plt.show()
I don't understand what you mean by this:
What I want to do is to plot the data, but on x-axis instead of depicting timestamps I want to print strings, such as: "name_1" , "name_2", "name_3".
What are these names? You said you hand two columns, only two things to plot; date and level. Where do these names come in?

But if you have a bunch of names, you can add them as a column to your dataframe and select the name column as one of the things that is plotted.
Reply
#3
Thanks for your time!! Ok, please look here: https://stackoverflow.com/questions/3100...xis-points at unutbu's code. I want to put names in x-axis, such as: "day_1", "day_2", "day_3",... instead of timestamps.... Did I help?
Reply
#4
Any ideas???
Reply
#5
Then you need to create a column that contains "day 1", "day 2", etc... These can probably be created using pandas datetime tools.
import pandas as pd
import random
import matplotlib.pyplot as plt


times = pd.date_range(start='2022-03-1', end='2022-03-21', periods=21)
levels = [random.randint(1, 10) for _ in times]

df = pd.DataFrame({"date": times, "level": levels})

df["days"] = (df["date"] - times[0]).dt.days.astype('int16')
df.plot(kind="line", x="days", y="level")
print(df)

plt.show()
hobbyist likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Date Time Series Help...Please spra8560 2 374 Feb-01-2024, 01:38 PM
Last Post: spra8560
  find the sum of a series of values that equal a number ancorte 1 504 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  Remove values for weekend in a panda series JaneTan 0 680 Dec-12-2022, 01:50 AM
Last Post: JaneTan
  Getting proper x,y axis values pyhill00 8 1,677 Jul-29-2022, 06:48 PM
Last Post: pyhill00
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,161 May-17-2022, 11:38 AM
Last Post: Larz60+
  Time series JasminQuinn 0 1,024 Apr-22-2022, 10:33 PM
Last Post: JasminQuinn
  How to read rainfall time series and insert missing data points MadsM 4 2,192 Jan-06-2022, 10:39 AM
Last Post: amdi40
Question How to print each possible permutation in a dictionary that has arrays as values? noahverner1995 2 1,758 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,390 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Put zero for x axis values quest 4 2,293 Jul-29-2021, 01:34 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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