1

I manage to create a graph using plot() on a certain pivot_table(). The problem is that the x axis indexes are to busy so you can't really read them (I added a picture).

Here is what i wrote:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
All_sensor_data = pd.read_csv("C:/Users/USER1/Desktop/Lab/TMP sensor/tot_data_TMP/All_sensor_data.csv")
All_sensor_data = All_sensor_data.rename(columns={"Date_n_Time": "datetime"})


TMP = All_sensor_data.pivot_table(index="datetime", columns="treatment", values="Temperature (*C)",aggfunc='mean', fill_value = 0)


TMP["2022-08-07":"2023-01-10"].plot(xlabel="Date", ylabel="Temperture (C*)",title = "Temperture vs Date")

script photo

here is the output:

script output - note the too bust x axis

how can i space the indexes labeling? I want a graph using this data with less written indexes on the x axis.

Thanks, Jonathan.

Klaus D.
  • 13,874
  • 5
  • 41
  • 48

2 Answers2

0

It is difficult to make sure that this will help you, since you did not give access to the csv file with your data. Therefore I cannot plot the data myself to see if this will work.

You can try matplotlib.pyplot_date(). This allows you to format the date axes, whether it is on the x-axis or on the y-axis. Use the help function in python to read more about the arguments, or follow this link.

  • This solution would be much more helpful with sample code. You can create fake dates and y-axis date to illustrate your approach. – Donna Mar 25 '23 at 01:27
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Donna Mar 25 '23 at 01:28
0

This sample code should do what you want. It will select every other date in your date array as x-tick marks. The date labels are then rotated and aligned at the left with the x-location of the labeled data.

import matplotlib.pyplot as plt
from datetime import date, timedelta

today = date.today()
dates = [str(today + timedelta(days=i)) for i in range(15)]

y = range(len(dates))
plt.plot(dates,y,'r.-',ms=10)

# Select every other date as tick marks; rotate date labels; 
plt.xticks(dates[::2],rotation=-25,horizontalalignment='left'); # or ha='left'

# Fix plot so that date labels are completely within plot area
plt.tight_layout()   

plt.show()

Plot with rotated dates as x-tick marks.

Donna
  • 1,390
  • 1
  • 14
  • 30