@@ -107,7 +107,7 @@ def _set_scale_type(channel, scale):
107107 elif scale ['type' ] == 'utc' :
108108 raise NotImplementedError
109109 elif scale ['type' ] == 'sequential' :
110- raise NotImplementedError
110+ raise NotImplementedError ( "sequential scales used primarily for continuous colors" )
111111 else :
112112 raise NotImplementedError
113113 return lims
@@ -133,19 +133,49 @@ def _set_tick_locator(channel, axis):
133133
134134
135135def _set_tick_formatter (channel , axis ):
136+ current_axis = {'x' : channel ['ax' ].xaxis , 'y' : channel ['ax' ].yaxis }
137+ format_str = ''
138+
139+ if 'format' in axis :
140+ format_str = axis ['format' ]
141+
136142 if channel ['dtype' ] == 'temporal' :
137- formatter = mdates .DateFormatter ('%b %d, %Y' )
143+ if not format_str :
144+ format_str = '%b %d, %Y'
145+
146+ current_axis [channel ['axis' ]].set_major_formatter (mdates .DateFormatter (format_str ))
138147
139- current_axis = {'x' : channel ['ax' ].xaxis , 'y' : channel ['ax' ].yaxis }
140- current_axis [channel ['axis' ]].set_major_formatter (formatter )
148+ try :
149+ current_axis [channel ['axis' ]].get_major_formatter ().__call__ (1 )
150+ except ValueError :
151+ raise ValueError ("Matplotlib only supports `strftime` formatting for dates."
152+ "Currently, %L, %Q, and %s are allowed in Altair, but not allowed in Matplotlib."
153+ "Please use a :func:`strftime` compliant format string." )
141154
155+
156+ # TODO: move rotation to another function?
142157 if channel ['axis' ] == 'x' :
143158 for label in channel ['ax' ].get_xticklabels ():
144159 # Rotate the labels on the x-axis so they don't run into each other.
145160 label .set_rotation (30 )
146161 label .set_ha ('right' )
162+
163+ elif channel ['dtype' ] == 'quantitative' :
164+ if format_str :
165+ current_axis [channel ['axis' ]].set_major_formatter (ticker .StrMethodFormatter ('{x:' + format_str + '}' ))
166+
167+ # Verify that the format string is valid for Matplotlib and exit nicely if not.
168+ try :
169+ current_axis [channel ['axis' ]].get_major_formatter ().__call__ (1 )
170+ except ValueError :
171+ raise ValueError ("Matplotlib only supports format strings as used by `str.format()`."
172+ "Some format strings that work in Altair may not work in Matplotlib."
173+ "Please use a different format string." )
174+ else :
175+ # Use the default formatter for quantitative (it has similar, if not the same settings as Altair)
176+ pass
147177 else :
148- pass # Use the auto formatter for quantitative (it has similar, if not the same settings as Altair)
178+ pass
149179
150180
151181def convert_axis (ax , chart ):
0 commit comments