Plot: Too many ticks on X ax
The first loaded area has too many ticks on X ax (see image 01).
If I use the zoom action on X ax the graph is now well loaded.
Can you give me some advice where I can look because the Plot constructor parameters seem to be good.
date_range = (735599.0, 735745.0) x = (735610.5, 735647.0, 735647.5, 735648.5, 735669.0, 735699.0, 735701.5, 735702.5, 735709.5, 735725.5, 735728.5, 735735.5, 735736.0) y = (227891.25361545716, 205090.4880046467, 208352.59317388065, 175462.99296699322, 98209.836461969651, 275063.37219361769, 219456.93600708069, 230731.12613806152, 209043.19805037521, 218297.51486296533, 208036.88967207001, 206311.71988471842, 216036.56824433553) y0 = 218206.79192 x_after = (735610.5, 735647.0, 735647.5, 735701.5, 735702.5, 735709.5, 735725.5, 735728.5, 735735.5, 735736.0) y_after = (227891.25361545716, 205090.4880046467, 208352.59317388065, 219456.93600708069, 230731.12613806152, 209043.19805037521, 218297.51486296533, 208036.88967207001, 206311.71988471842, 216036.56824433553) linex = -39.1175584541 liney = 28993493.5251 ax.plot_date(x, numpy.array(y) / y0, color='r', xdate=True, marker='x') ax.plot_date(x_after, numpy.array(y_after) / y0, color='r', xdate=True) ax.set_xlim(date_range) steps = list(ax.get_xlim()) steps.append(steps[-1] + 2) steps = [steps[0] - 2] + steps ax.plot(steps, numpy.array([linex * a + liney for a in steps]) / y0, color='b')
Thank you for your help. Manuel
source to share
I'm not sure if I understand exactly what you want to do, but is it turning your xticklabels good enough for you?
# Add this code at the end of your script
# It will rotate the labels contained in your date range
plt.xticks(rotation=70)
If I test your code, I have 7 labels, but the rotation argument changes to 0 (horizontal)
source to share
If you have too many xtick
labels
, so many that they are all grouped by plot, you can shrink them by using pyplot.xticks
. the arguments are the points to which the labels are applied, the labels themselves, and the optional rotation.
import numpy as np import matplotlib.pyplot as plt y = np.arange(10000) ticks = y - 5000 plt.plot(y) k = 1000 ys = y[::k] ys = np.append(ys, y[-1]) labels = ticks[::k] labels = np.append(labels, ticks[-1]) plt.xticks(ys,labels, rotation='vertical') plt.show() plt.close()
source to share