meaningful labeling of the x-axis (every 30 minutes)

Question:

Let’s say I have python plot looking like this:

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import pandas as pd
import seaborn as sns

data= pd.read_csv('data.csv')

df=pd.DataFrame(data)

A = list(df.iloc[:,5])
B = list(df.iloc[:,7])

plt.figure(figsize=(30,10))

sns.lineplot(x=A, y=B, color='b')

# Setting Ticks
plt.minorticks_on()
plt.grid(b=True, which='major', color='#999999', linestyle='-', alpha=0.2)
plt.tick_params(axis='x',labelsize=10,rotation=90)
plt.xlabel("Time", fontsize=14,fontweight='bold')
plt.ylabel("Ion Current (A)", fontsize=14,fontweight='bold')
blue_patch = mpatches.Patch(color='b', label='2°C')
plt.legend(handles=[blue_patch])

plt.show()

data.csv contains a column, which look like this:

12:01:00
12:02:00
12:03:00
12:04:00
12:05:00
.
.
.
.
12:31:00
.
.
.
.
13:01:00

this time column is plotted on the x-axis, which leads to the labeling for each minute. But how is it possible to label only every 30 minutes, that the x-axis reads 12:01:00 12:31:00 13:01:00?

Asked By: Marc

||

Answers:

to set the number of ticks while allowing matplotlib to position them You can use pyplot.locator_params

pyplot.locator_params(nbins=4)

to use it in an specific axis

pyplot.locator_params(axis='x', nbins=30)
Answered By: Lucas M. Uriarte
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.