Python Forum
Print names in x-axis of a time-series values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Print names in x-axis of a time-series values (/thread-39846.html)



Print names in x-axis of a time-series values - hobbyist - Apr-22-2023

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?


RE: Print names in x-axis of a time-series values - deanhystad - Apr-22-2023

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.


RE: Print names in x-axis of a time-series values - hobbyist - Apr-22-2023

Thanks for your time!! Ok, please look here: https://stackoverflow.com/questions/3100985/plot-with-custom-text-for-x-axis-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?


RE: Print names in x-axis of a time-series values - hobbyist - Apr-22-2023

Any ideas???


RE: Print names in x-axis of a time-series values - deanhystad - Apr-22-2023

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()