-1

Here is my code and plot that I am getting. I want to only have like 5 or 6 xticks but can't seem to do so.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as md
import matplotlib.dates

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

columns = ["Time", "SO2_inlet",'SO2_outlet']
df = pd.read_csv("../code_thesis/0713_.25g.csv", usecols=columns)
print("Contents in csv file:\n", df)
plt.plot(df.Time, df.SO2_inlet, df.SO2_outlet)

enter image description here

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

1 Answers1

0

The reason you are seeing that many entries in the x-axis is because you are reading the csv file with date (or time) content as text. If you try df.info(), you will see the Time column as object.

So, you need to convert the Time column into datetime using pd.to_datetime(). Also, you will need to format the data that will be shown in the X-axis using DateFormatter() and 'set_major_formatter'. The below code has an example code to help you. But, as you have not provided the data that is used for the plot, you may need to refer to the information here to change it to time format, year format, etc. as you it.

df=pd.read_csv('Timetext.csv') #Reading my data
print(df.head())

df['Time'] = pd.to_datetime(df['Time'], format="%d-%m-%Y") # EDIT this based on how your CSV text data is
print(df.info())
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

columns = ["Time", "SO2_inlet",'SO2_outlet']
plt.plot(df.Time, df.SO2_inlet)
plt.plot(df.Time, df.SO2_outlet)
# Define the date format
from matplotlib.dates import DateFormatter
date_form = DateFormatter("%d-%b-%y") ## Change this to what you want to show in X-axis DATE labels
plt.gca().xaxis.set_major_formatter(date_form)

Output

## MY CSV data ##
         Time  SO2_inlet  SO2_outlet
0  01-01-2022        645         352
1  02-01-2022        104         917
2  03-01-2022        236         630
3  04-01-2022        510         901
4  05-01-2022        781         287

## CSV data changes to datetime64 ##
 #   Column      Non-Null Count  Dtype         
---  ------      --------------  -----         
 0   Time        195 non-null    datetime64[ns]
 1   SO2_inlet   195 non-null    int64         
 2   SO2_outlet  195 non-null    int64   

enter image description here

Redox
  • 9,321
  • 5
  • 9
  • 26