ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 27 Apr 2020 20:59:05 +0200Adapt 2D-plot tick label attributes to dateshttps://ask.sagemath.org/question/51080/adapt-2d-plot-tick-label-attributes-to-dates/Is there a way to access ticks labels to rotate them? I've tried `Graphics.matplotlib()` and `set_rotation()`, but this doesn't seem to produce changes. Am I doing wrong things?
In the example below, the formatter and locator are working correctly, but the problem is all labels are oriented horizontally, messing all together since each tick label is a date and therefore quite long. Need to rotate them.
import csv
from datetime import datetime
from matplotlib import ticker
from matplotlib import dates
data = [('04/22/20', '04/23/20', '04/24/20','04/25/20','04/26/20', '04/27/20'),
(20, 40, 80, 160, 320, 640)]
labels = data[0]
labels = map(lambda x: dates.date2num(datetime.strptime(x, '%m/%d/%y')), labels)
labels = list(labels)
values = data[1]
values = map(lambda x: int(x), values)
# Z is a list of [(x1, y1), (x2, y2)...]
# x1, x2, ... are dates
# y1, y2, ... are values
Z = zip(labels, values)
Z = list(Z)
p = list_plot(Z, ticks=[1, None],
tick_formatter=[dates.DateFormatter('%d.%m.%Y'), None],
axes_labels=['Days', '$\\log \\;{N}$'], plotjoined=True,
thickness=2, figsize=4, scale='semilogy')
G = p.matplotlib()
labels = G.axes[0].xaxis.get_ticklabels()
labels = list(labels)
for label in labels:
label.set_rotation(45)
p
This outputs the plot with an ugly x-axis on which all the dates are messed up. How to fix that?
![Plot with dates as tick labels on the x-axis](https://i.imgur.com/14VIFv4.png)MrDvbnhbqMon, 27 Apr 2020 20:59:05 +0200https://ask.sagemath.org/question/51080/