Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading