0

I am in a fix and I need your help. I have searched every available potential solution to no avail. I am trying to format plot B in the same way as plot A. I want plot B to have an origin visualized in the same way A does. The x-axis of plot B is scanty; I want more days displayed just like in plot A. Any help will be most appreciated. Thanks.

Plots A and B

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

fig = plt.figure(figsize=(20,15))   
ax = plt.axes() 

# Add three axes to the plots
ax1 = plt.subplot(2, 2, 1)
ax2 = plt.subplot(2, 2, 2)

# Select pandas dataframe columns and define a line plot for PM10, PM2.5 and PM1.0 each
df_mushin.filter(['PM10']).plot(ax=ax1, style='o-', color='r', legend=False, label='PM₁₀')
df_mushin.filter(['PM2.5']).plot(ax=ax1, style='o-', color='b', legend=False, label='PM₂.₅')
df_mushin.filter(['PM1']).plot(ax=ax1, style='o-', color='k', label='PM₁')

ax1.axhline(y=45, color='r', linestyle='dotted', label='PM₁₀ WMO daily threshold')
ax1.axhline(y=15, color='b', linestyle='dotted', label='PM₂.₅ WMO daily threshold')
ax1.set_title('\n Mushin - April', fontsize=16, pad=16)
ax1.set_ylabel('Particulate matter (PM) in µg/m³', fontsize=16)
ax1.set_yticks(np.arange(30, 215, 30))
ax1.set_yticks(np.arange(0, 215, 15), minor=True)
ax1.legend(fontsize=14,loc=0)

# Select pandas dataframe columns and define a line plot for PM10, PM2.5 and PM1.0 each
df_amuwo.filter(['Amuwo-PM10']).plot(ax=ax2, style='o-', color='r', legend=False)
df_amuwo.filter(['Amuwo-PM2.5']).plot(ax=ax2, style='o-', color='b', legend=False)
df_amuwo.filter(['Amuwo-PM1']).plot(ax=ax2, style='o-', color='k', legend=False)

ax2.axhline(y=45, color='r', linestyle='dotted')
ax2.axhline(y=15, color='b', linestyle='dotted')
ax2.set_title('\n Amuwo - August', fontsize=16, pad=16)
ax2.set_yticks(np.arange(30, 215, 30))
ax2.set_yticks(np.arange(0, 215, 15), minor=True)

plt.show()
Deeone
  • 133
  • 1
  • 3
  • 11
  • Please can you provide data to reproduce your plots. – Corralien Jan 30 '23 at 07:07
  • Here is the data used: https://docs.google.com/spreadsheets/d/169AtYxOXFnE-0ZRLEo66xsPTtKW-wSTX/edit?usp=sharing&ouid=112433239425425901051&rtpof=true&sd=true – Deeone Jan 30 '23 at 07:29

1 Answers1

1

Add the following to your code:

from matplotlib.dates import ConciseDateFormatter, DayLocator
from matplotlib.ticker import NullLocator, NullFormatter

locator = DayLocator(interval=1)
formatter = ConciseDateFormatter(locator)
ax_mushin.xaxis.set_major_locator(locator)
ax_mushin.xaxis.set_major_formatter(formatter)
ax_mushin.xaxis.set_minor_locator(NullLocator())
ax_mushin.xaxis.set_minor_formatter(NullFormatter())

locator = DayLocator(interval=2)
formatter = ConciseDateFormatter(locator)
ax_amuwo.xaxis.set_major_locator(locator)
ax_amuwo.xaxis.set_major_formatter(formatter)
ax_amuwo.xaxis.set_minor_locator(NullLocator())
ax_amuwo.xaxis.set_minor_formatter(NullFormatter())

plot with concise xaxis

paime
  • 2,901
  • 1
  • 6
  • 17
  • The code worked, but I am unable to show the plot, I am getting this error. Any idea what might be the problem? Cell In[13], line 151 plt.savefig('Lagos_Air-Quality_PM.png', format = 'png', dpi = 300) ^ SyntaxError: invalid syntax – Deeone Jan 30 '23 at 08:41
  • It's a `SyntaxError`, check your parentheses, quotes, commas, etc. – paime Jan 30 '23 at 08:46
  • Yes, but the code seems ok, the error is coming from this. `plt.savefig('Lagos_Air-Quality_PM.png', format = 'png', dpi = 300) plt.show()` – Deeone Jan 30 '23 at 08:49
  • see the image hear, I have checked and checked. What could possibly be wrong? https://drive.google.com/file/d/1tGfKidRrGywiMq0eHp5o95AYZjJFp9MB/view?usp=sharing – Deeone Jan 30 '23 at 08:57
  • You need `plt.show()` on its own line. – paime Jan 30 '23 at 08:57
  • Delete the line, see if there is still the `SyntaxError` – paime Jan 30 '23 at 08:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/251488/discussion-between-paime-and-deeone). – paime Jan 30 '23 at 08:58