0

I am trying to use this code:

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0,1,2,3])
y = np.array([20,21,22,23])
my_xticks = ['John','Arnold','Mavis','Matt']
plt.xticks(x, my_xticks)
plt.plot(x, y)
plt.show()

(source: plot with custom text for x axis points)

in time-series .csv where I have 2 columns. The one column has timestamps and the other column has temperatures. I need in the plot, and more specifically in the x-axis instead of timestampts' values to have something like this: "day1", "day2", "day3",... Any ideas? I am trying to approach it but all my efforts are very dissapointing, since I haven't managed to realized it.

>>>> UPDATE (23/4/2023): <<<<

This is the code I use:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = pd.read_csv("measurements.csv", sep=",")

use_days = [1, 2, 3, 4, 5, 6, 7] # <-- put here the days numbers to show

xticks_labels = [f"day{i+1}" for i, _ in enumerate(df["Date"])]
selected_labels = [xticks_labels[idx] for idx in [i-1 for i in use_days]]

plt.xticks(df["Date"].iloc[[i-1 for i in use_days]], selected_labels, rotation=45, fontsize=12) #adjust those values if needed

plt.show();

And I get this error:

ConversionError: Failed to convert value(s) to axis units:

just_learning
  • 413
  • 2
  • 11
  • 24

1 Answers1

1

IIUC, you can try this :

np.random.seed(123456)

df = pd.DataFrame({"timestamp": pd.date_range("20230101", periods=10, freq="D"),
                   "temperature": np.random.normal(loc=30, scale=5, size=10)})

plt.plot(df["timestamp"], df["temperature"])

plt.xticks(df["timestamp"], [f"day{i+1}" for i, _ in enumerate(df["timestamp"])],
           rotation=45, fontsize=10) #adjust those values if needed

plt.show();

Output :

enter image description here

Update :

If you need to show only some specific days, use this :

use_days = [2, 5, 8] # <-- put here the days numbers to show

xticks_labels = [f"day{i+1}" for i, _ in enumerate(df["timestamp"])]
selected_labels = [xticks_labels[idx] for idx in [i-1 for i in use_days]]

plt.xticks(df["timestamp"].iloc[[i-1 for i in use_days]],
           selected_labels, rotation=45, fontsize=12) #adjust those values if needed

plt.show();

Output :

enter image description here

DF used :

   timestamp  temperature
0 2023-01-01    32.345561
1 2023-01-02    28.585683
2 2023-01-03    22.454707
3 2023-01-04    24.321838
4 2023-01-05    36.060560
5 2023-01-06    29.133927
6 2023-01-07    30.596044
7 2023-01-08    24.778820
8 2023-01-09    25.690755
9 2023-01-10    19.477154
Timeless
  • 22,580
  • 4
  • 12
  • 30
  • Thanks!!!! where do I read the .csv? – just_learning Apr 22 '23 at 21:57
  • 1
    Replace `df = pd.DataFrame(...)` by `df = pd.read_csv("path_of_your_csv", sep=",", ...)`. – Timeless Apr 22 '23 at 21:59
  • Amazing code!!! Thanks a lot! It prints all the values. So If I have 1000 timestamps, how can I make it to print day1...day10 and not day1...day1000? I changed ```day{i+1}``` to ```day{i+100}``` but it didn't change... – just_learning Apr 22 '23 at 22:21
  • 1
    I'm not sure to understand you request. How do you to print only 10 days/xticks when you have 1000 ? – Timeless Apr 22 '23 at 22:27
  • Yes, sorry! I need to do a sampling, so not print all the days, only the ***number*** of days I want...Can I do that? So If I have 100 days, to print only every 10 days! Day10, day20,...,day100. – just_learning Apr 22 '23 at 22:31
  • 1
    I updated my answer, can you check it out ? If it does not fulfill your question, you need to share a reproducible example with the matching expected output. – Timeless Apr 22 '23 at 22:44
  • Please see above my update with the code + the error I use....Thanks – just_learning Apr 23 '23 at 12:02
  • 1
    We still don't know how look like your `df`. Can you `print(df.sample(10).to_dict("list"))` and add it to your question ? – Timeless Apr 23 '23 at 12:03