简体   繁体   中英

How to set custom x-axis and y-axis ticks in matplotlib?

This is the graph I obtained from the code shown below (this is a snippet of a much larger script)

dataset = pd.read_csv('mon-ac-on-uni-on.csv')
print(dataset.columns)
X_test_mon = dataset[['Day', 'Month', 'Hour', 'AirConditioning', 'Temp','Humidity', 'Calender','Minute']]
y_test_mon = dataset.loc[:, 'AVG(totalRealPower)'].values
print(X_test_mon.columns)

y_pred_mon=regr.predict(X_test_mon)

plt.plot(y_test_mon, color = 'red', label = 'Real data')
plt.plot(y_pred_mon, color = 'blue', label = 'Predicted data')
plt.title('Random Forest Prediction- MONDAY- AC-ON-Uni-ON')
plt.legend()
plt.xlabel('Time')
plt.ylabel('Watt')
plt.show()

As you can see it has rows count on x-axis and power in watt on y-axis

now I want to have only time (Hour) ticks (8 - 17) on x-axis and power in KW (ie divided by 1000) plotted on the y-axis. For achieving that I tried following

plt.xticks(X_test_mon['Hour'])
plt.yticks(np.round(y_test_mon/1000))

but what I got is shown below: just black square on both the axes

I also tried

plt.xticks(range(8,17))

but no change. I am lost here. Please help!

As far as i can see, the results from y_test_mon and y_pred_mon are plotted against the "index" of the respective dataset. From the line, where X_test_mon is defined I would suspect, that the smallest timestep between each datapoint in the plot is 1 hour.

Right now the plot is drawn for the whole monitoring timespan. Try the following:

dates = X_test_mon.groupby(['Day','Month']).groups.keys()


for day, month in dates:
    fig, ax = plt.subplots()
    daily_avg_test_data = y_test_mon[(y_test_mon['Day'] == day) & (y_test_mon['Month'] == month)]
    daily_avg_pred_data = y_pred_mon[(y_test_mon['Day'] == day) & (y_test_mon['Month'] == month)]
    daily_avg_test_data.plot(x='Hour', y='AVG(totalRealPower)', ax=ax)
    daily_avg_pred_data.plot(x='Hour', y='AVG(totalRealPower)', ax=ax)
    plt.xlabel('Time')
    plt.ylabel('kW')
 
    # values were selected from the provided image, should fit the actual plotted data range
    major_ticks=np.arange(20000, 120000, 20000)

    # for plt.yticks(actual, replacement) you have to provide the actual tick (data) values and then the 
    # "replacement" values
    plt.yticks(major_ticks, major_ticks/1000)
plt.show()

This should generate multiple figures (one for each day) that contain hourly data and y-axis scaling in kW.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM