-1

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?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Marc
  • 25
  • 5
  • 1
    if you can place datetime column as index, - you can use `resample("30min")` function – NoobVB Sep 05 '22 at 08:43
  • Welcome to SO. Questions like this have many duplicates here already with answers, before you ask a duplicate please use the search button to find the duplicates. – smci Sep 05 '22 at 17:11

1 Answers1

1

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)
Lucas M. Uriarte
  • 2,403
  • 5
  • 19