ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 28 Apr 2020 11:57:35 -0500How to add string tick labels to 2D plots?http://ask.sagemath.org/question/9945/how-to-add-string-tick-labels-to-2d-plots/Hi folks, i want to add string tick labels to a 2D plot, e.g. `['$100', '$200', '$300']` on the x-axis instead of the ticks `[1, 2, 3]`. I see from [http://stackoverflow.com/questions/5439708/python-matplotlib-creating-date-ticks-from-string](http://stackoverflow.com/questions/5439708/python-matplotlib-creating-date-ticks-from-string) that i can do this with matplotlib via
from matplotlib import pyplot as plt
plt.plot([1,2,3],[3,4,3])
ax = plt.gca()
ax.set_xticks([1,2,3])
ax.set_xticklabels(['$100','$200','$300'])
Is there a Sage way to accomplish the same thing?
Personally, i find Sage plotting commands more user-friendly than matplotlib commands.Sun, 24 Mar 2013 12:48:21 -0500http://ask.sagemath.org/question/9945/how-to-add-string-tick-labels-to-2d-plots/Comment by slelievre for <p>Hi folks, i want to add string tick labels to a 2D plot, e.g. <code>['$100', '$200', '$300']</code> on the x-axis instead of the ticks <code>[1, 2, 3]</code>. I see from <a href="http://stackoverflow.com/questions/5439708/python-matplotlib-creating-date-ticks-from-string">http://stackoverflow.com/questions/5439708/python-matplotlib-creating-date-ticks-from-string</a> that i can do this with matplotlib via</p>
<pre><code>from matplotlib import pyplot as plt
plt.plot([1,2,3],[3,4,3])
ax = plt.gca()
ax.set_xticks([1,2,3])
ax.set_xticklabels(['$100','$200','$300'])
</code></pre>
<p>Is there a Sage way to accomplish the same thing? </p>
<p>Personally, i find Sage plotting commands more user-friendly than matplotlib commands.</p>
http://ask.sagemath.org/question/9945/how-to-add-string-tick-labels-to-2d-plots/?comment=51116#post-id-51116For reference, a related question was later asked:
- [Ask Sage question 51080: rotate tick labels](https://ask.sagemath.org/question/51080)Tue, 28 Apr 2020 11:56:25 -0500http://ask.sagemath.org/question/9945/how-to-add-string-tick-labels-to-2d-plots/?comment=51116#post-id-51116Answer by MrDvbnhbq for <p>Hi folks, i want to add string tick labels to a 2D plot, e.g. <code>['$100', '$200', '$300']</code> on the x-axis instead of the ticks <code>[1, 2, 3]</code>. I see from <a href="http://stackoverflow.com/questions/5439708/python-matplotlib-creating-date-ticks-from-string">http://stackoverflow.com/questions/5439708/python-matplotlib-creating-date-ticks-from-string</a> that i can do this with matplotlib via</p>
<pre><code>from matplotlib import pyplot as plt
plt.plot([1,2,3],[3,4,3])
ax = plt.gca()
ax.set_xticks([1,2,3])
ax.set_xticklabels(['$100','$200','$300'])
</code></pre>
<p>Is there a Sage way to accomplish the same thing? </p>
<p>Personally, i find Sage plotting commands more user-friendly than matplotlib commands.</p>
http://ask.sagemath.org/question/9945/how-to-add-string-tick-labels-to-2d-plots/?answer=51050#post-id-51050Is 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?
# Z is a list of [(x1,y1),(x2,y2)...]
# x1,x2 are dates
# The formatter and locator are working correctly, but the problem is all labels are oriented horizontally, messing all together. Need to rotate them
p = list_plot( Z, ticks=[10, None], tick_formatter=[dates.DateFormatter('%d.%m.%Y'), None], axes_labels=[ 'Days', '$\\log \\;{N}$' ], scale='semilogy' )
G = p.matplotlib()
labels = G.axes[0].xaxis.get_ticklabels()
labels = list(labels)
for label in labels:
label.set_rotation(45)Mon, 27 Apr 2020 02:50:37 -0500http://ask.sagemath.org/question/9945/how-to-add-string-tick-labels-to-2d-plots/?answer=51050#post-id-51050Comment by slelievre for <p>Is there a way to access ticks labels to rotate them? I've tried <code>Graphics.matplotlib()</code> and <code>set_rotation()</code>, but this doesn't seem to produce changes. Am I doing wrong things?</p>
<pre><code># Z is a list of [(x1,y1),(x2,y2)...]
# x1,x2 are dates
# The formatter and locator are working correctly, but the problem is all labels are oriented horizontally, messing all together. Need to rotate them
p = list_plot( Z, ticks=[10, None], tick_formatter=[dates.DateFormatter('%d.%m.%Y'), None], axes_labels=[ 'Days', '$\\log \\;{N}$' ], scale='semilogy' )
G = p.matplotlib()
labels = G.axes[0].xaxis.get_ticklabels()
labels = list(labels)
for label in labels:
label.set_rotation(45)
</code></pre>
http://ask.sagemath.org/question/9945/how-to-add-string-tick-labels-to-2d-plots/?comment=51117#post-id-51117@MrDvbnhbq you can maybe delete your answer here.
I added a comment under the question here to point to
the question you asked.Tue, 28 Apr 2020 11:57:35 -0500http://ask.sagemath.org/question/9945/how-to-add-string-tick-labels-to-2d-plots/?comment=51117#post-id-51117Comment by MrDvbnhbq for <p>Is there a way to access ticks labels to rotate them? I've tried <code>Graphics.matplotlib()</code> and <code>set_rotation()</code>, but this doesn't seem to produce changes. Am I doing wrong things?</p>
<pre><code># Z is a list of [(x1,y1),(x2,y2)...]
# x1,x2 are dates
# The formatter and locator are working correctly, but the problem is all labels are oriented horizontally, messing all together. Need to rotate them
p = list_plot( Z, ticks=[10, None], tick_formatter=[dates.DateFormatter('%d.%m.%Y'), None], axes_labels=[ 'Days', '$\\log \\;{N}$' ], scale='semilogy' )
G = p.matplotlib()
labels = G.axes[0].xaxis.get_ticklabels()
labels = list(labels)
for label in labels:
label.set_rotation(45)
</code></pre>
http://ask.sagemath.org/question/9945/how-to-add-string-tick-labels-to-2d-plots/?comment=51082#post-id-51082If that would be a better way to ask questions,
[https://ask.sagemath.org/question/51080/how-to-change-ticks-labels-attributes-for-the-2d-plot/](https://ask.sagemath.org/question/51080/how-to-change-ticks-labels-attributes-for-the-2d-plot/)
Any help is much appreciated.Mon, 27 Apr 2020 14:08:44 -0500http://ask.sagemath.org/question/9945/how-to-add-string-tick-labels-to-2d-plots/?comment=51082#post-id-51082Comment by Sébastien for <p>Is there a way to access ticks labels to rotate them? I've tried <code>Graphics.matplotlib()</code> and <code>set_rotation()</code>, but this doesn't seem to produce changes. Am I doing wrong things?</p>
<pre><code># Z is a list of [(x1,y1),(x2,y2)...]
# x1,x2 are dates
# The formatter and locator are working correctly, but the problem is all labels are oriented horizontally, messing all together. Need to rotate them
p = list_plot( Z, ticks=[10, None], tick_formatter=[dates.DateFormatter('%d.%m.%Y'), None], axes_labels=[ 'Days', '$\\log \\;{N}$' ], scale='semilogy' )
G = p.matplotlib()
labels = G.axes[0].xaxis.get_ticklabels()
labels = list(labels)
for label in labels:
label.set_rotation(45)
</code></pre>
http://ask.sagemath.org/question/9945/how-to-add-string-tick-labels-to-2d-plots/?comment=51076#post-id-51076If you want to ask a question, I suggest that you start a new question instead of putting the question inside an answer to a question from 7 years ago.Mon, 27 Apr 2020 13:20:15 -0500http://ask.sagemath.org/question/9945/how-to-add-string-tick-labels-to-2d-plots/?comment=51076#post-id-51076Answer by ppurka for <p>Hi folks, i want to add string tick labels to a 2D plot, e.g. <code>['$100', '$200', '$300']</code> on the x-axis instead of the ticks <code>[1, 2, 3]</code>. I see from <a href="http://stackoverflow.com/questions/5439708/python-matplotlib-creating-date-ticks-from-string">http://stackoverflow.com/questions/5439708/python-matplotlib-creating-date-ticks-from-string</a> that i can do this with matplotlib via</p>
<pre><code>from matplotlib import pyplot as plt
plt.plot([1,2,3],[3,4,3])
ax = plt.gca()
ax.set_xticks([1,2,3])
ax.set_xticklabels(['$100','$200','$300'])
</code></pre>
<p>Is there a Sage way to accomplish the same thing? </p>
<p>Personally, i find Sage plotting commands more user-friendly than matplotlib commands.</p>
http://ask.sagemath.org/question/9945/how-to-add-string-tick-labels-to-2d-plots/?answer=14688#post-id-14688This functionality is not yet in sage-5.8, but will be in sage-5.9. If you want it *now* then simply use sage-5.9.beta1, or patch ticket 13078 into your own version of sage.Sun, 24 Mar 2013 15:05:01 -0500http://ask.sagemath.org/question/9945/how-to-add-string-tick-labels-to-2d-plots/?answer=14688#post-id-14688