Im trying to do a scatter plot, when i try to do it, the x axis show a lot of dates. There is a way to put only a few of dates or years in the x axis shown in the scatter plot?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.dates as mdates
plt.scatter(terremoto_sur['time'],terremoto_sur['mag'])
plt.title('Magnitud terremotos en el tiempo zona Sur')
plt.xlabel('Time')
plt.ylabel('Magnitude')
plt.show()
Here is the scatter plot with the problem
CodePudding user response:
It is difficult to tell without seeing the dtype of columns, but what is likely happening is that the 'time' column is not a time-specific pandas datatype like Timestamp or DatetimeIndex. For example, If 'time' is stored with a dtype of object
then each time data point is essentially a string, and matplotlib simply plots each one with its own tick mark with the string under the tick.
You should check the dtype of your columns, and if necessary, convert the dtype with something like the to_datetime
method.
CodePudding user response:
Try this to set even spacing
plt.gca().xaxis.set_major_locator(plt.MultipleLocator(1))
You can also try this for sampling every other tick
ax = plt.gca()
ax.set_xticks(ax.get_xticks()[::2])