3

I have a DataFrame named airtep_df with columns 'AIRTEP' (Int64), 'AIRTEP_qc' (Int64), and 'UTCTime'. I'm trying to create a plot from this data, but the time tick labels on the x-axis are overlapping, making the plot unreadable. I attempted to rotate the tick labels using plt.xticks(rotation=45), but the plot disappeared. Can someone explain what went wrong and provide a solution for formatting the time tick labels with datetime correctly?

This picture shows the details about the data frame I'm using:

enter image description here

This is the script I used:

import matplotlib.pyplot as plt
import pandas as pd


index = (airtep_df['AIRTEP'] != 9999) & (airtep_df['AIRTEP_qc'] == 1)
selected_y = airtep_df.loc[index,'AIRTEP']
y = selected_y.values
selected_x = time_df.loc[index,'UTCTime']
x = selected_x.values
plt.plot(x[30],y[30])

#This is the result I have, the the x-axis are overlapping:enter image description here

#Then I trid to rotate the xticks

plt.xticks(rotation=45)

This is the result I have, as the picture:

enter image description here

How can I plot datetime data and format it such that the labels are not overlapping?

2
  • 1
    Have you tried using plt.setp as suggested in this answer?
    – tax evader
    Sep 22 at 4:21
  • 1
    It looks like your command to rotate the sticks was not operating on the original figure you made. Perhaps you closed the original figure before trying this, and so it has just created a new figure? You could try using the explicit rather than implicit interface, to ensure you are operating on the correct figure instance:
    – tmdavison
    Sep 22 at 13:49

1 Answer 1

1

Datetime-formatted rotated x-axis tick labels

You could try something like:

import numpy as np
import pandas as pd
​
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
​
​
### Sample data generation
date_rng = pd.date_range(start="2016-01-01", end="2016-01-02", freq="H")
​
temps = np.random.randint(17, 41, size=(len(date_rng)))
​
qc_values = np.ones(len(date_rng), dtype=int)
​
airtep_df = pd.DataFrame(
    {"UTCTime": date_rng, "AIRTEP": temps, "AIRTEP_qc": qc_values}
)
​
print(airtep_df)
​
​
### Plotting
plt.figure(dpi=300)
plt.plot(airtep_df.UTCTime, airtep_df.AIRTEP, marker="o", linestyle="-")
​
# Rotating the x-tick labels
plt.xticks(rotation=45)
​
# Adjusting the date format
ax = plt.gca()
ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))
​
plt.tight_layout()
plt.xlabel("Time")
plt.ylabel("Temperature (℃)")
plt.grid(True, which="both", linestyle="--", linewidth=0.5)
plt.show()

which produces:

               UTCTime  AIRTEP  AIRTEP_qc
0  2016-01-01 00:00:00      30          1
1  2016-01-01 01:00:00      27          1
2  2016-01-01 02:00:00      37          1
3  2016-01-01 03:00:00      36          1
4  2016-01-01 04:00:00      18          1
5  2016-01-01 05:00:00      26          1
6  2016-01-01 06:00:00      27          1
7  2016-01-01 07:00:00      20          1
8  2016-01-01 08:00:00      27          1
9  2016-01-01 09:00:00      30          1
10 2016-01-01 10:00:00      37          1
11 2016-01-01 11:00:00      32          1
12 2016-01-01 12:00:00      29          1
13 2016-01-01 13:00:00      29          1
14 2016-01-01 14:00:00      23          1
15 2016-01-01 15:00:00      24          1
16 2016-01-01 16:00:00      37          1
17 2016-01-01 17:00:00      19          1
18 2016-01-01 18:00:00      19          1
19 2016-01-01 19:00:00      26          1
20 2016-01-01 20:00:00      35          1
21 2016-01-01 21:00:00      29          1
22 2016-01-01 22:00:00      24          1
23 2016-01-01 23:00:00      21          1
24 2016-01-02 00:00:00      39          1

Matplotlib plot with custom formatted datetime x-axis tick labels

1
  • I think there may have some issues relate to the version of each package. It works on my friend computer but not mine, thanks Sep 24 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.