0

Is it possible to change the color of a text in 3D plot. My question is similar to this Question Partial coloring of text in matplotlib but instead of 2d plot I am using 3D plot.

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import transforms

fig = plt.figure()
ax = Axes3D(fig)
x =1
y =2
z =3
ax.set_xlim3d(0,5)
ax.set_ylim3d(0,5)
ax.set_zlim3d(0,5)
ax.set_xticks(range(5))
ax.set_yticks(range(5))
ax.set_zticks(range(5))

t=ax.transData

ax.scatter([x], [y], [z], c='r', marker='*', s=500)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

ls="Data Point".split()

lc=['red','orange']
x=0.5
y=0.5
z=0.5


for s,c in zip(ls,lc):
        try:
            text = ax.text(x,y,z," "+s+" ",color=c, transform=t,size=50)
            text.draw(fig.canvas.get_renderer())
            ex = text.get_window_extent()
            t = transforms.offset_copy(text._transform, y=ex.height,x=ex.width, units='dots')

        except:
            print "error"



plt.show()
Community
  • 1
  • 1
jokeroor
  • 63
  • 1
  • 5
  • what code have you tried till now? – Faultier Feb 26 '16 at 08:19
  • I have updated my Question. I know my code have some basic errors so I had not posted it earlier. – jokeroor Feb 26 '16 at 08:40
  • for me your code runs perfectly fine if you remove the last three lines in your try statement. – Faultier Feb 26 '16 at 08:47
  • I removed those lines but words "Data" and "Point" gets overlapped – jokeroor Feb 26 '16 at 08:58
  • OK, I see your problem. The base renderer of your Text instance does not exist prior to the canvas actually getting drawn. I tried http://matplotlib.org/faq/howto_faq.html#automatically-make-room-for-tick-labels trick to hook it up with the draw event, but it freezes the picture (drawing successfully, though). In principle you try to force the text to have a renderer in the three lines of your try statement; I am not sure why that does not work. – Faultier Feb 26 '16 at 09:18

0 Answers0