Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot : Too many ticks on X axe

The first loaded plot have too many ticks on X axe (see image01).

enter image description here

If I use the zoom action on X axe, the plot is now well loaded.

enter image description here

Can you give me some advise where I can search because The Plot constructor parameters seems 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

like image 240
Manuel Campomanes Avatar asked Jul 08 '15 13:07

Manuel Campomanes


People also ask

How do I add more x-axis to a tick?

Create x and y points using numpy. Plot x and y points over the plot, where x ticks could be from 1 to 10 (100 data points) on the curve. To add extra ticks, use xticks() method and increase the range of ticks to 1 to 20 from 1 to 10. To display the figure, use the show() method.

How do I get rid of X ticks in MatPlotLib?

To remove the ticks on the x-axis, tick_params() method accepts an attribute named bottom, and we can set its value to False and pass it as a parameter inside the tick_params() function. It removes the tick on the x-axis.


1 Answers

If you have too many xtick labels, so many that they are all munged together on the plot, you can reduce them using pyplot.xticks. the arguments are the points the labels apply to, the labels themselves and an 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()

enter image description here

like image 190
wwii Avatar answered Sep 22 '22 15:09

wwii