1
1
# -*- coding: utf-8 -*-
2
2
#
3
- from . import color
4
-
5
3
import matplotlib as mpl
6
4
import numpy
7
5
6
+ from . import color
7
+
8
8
9
9
class Axes (object ):
10
10
def __init__ (self , data , obj ):
@@ -174,6 +174,7 @@ def __init__(self, data, obj):
174
174
# Unfortunately, _tickdir doesn't seem to be quite accurate. See
175
175
# <https://github.com/matplotlib/matplotlib/issues/5311>.
176
176
# For now, just take the first tick direction of each of the axes.
177
+ # pylint: disable=protected-access
177
178
x_tick_dirs = [tick ._tickdir for tick in obj .xaxis .get_major_ticks ()]
178
179
y_tick_dirs = [tick ._tickdir for tick in obj .yaxis .get_major_ticks ()]
179
180
if x_tick_dirs or y_tick_dirs :
@@ -200,20 +201,18 @@ def __init__(self, data, obj):
200
201
201
202
# Set each rotation for every label
202
203
x_tick_rotation_and_horizontal_alignment = \
203
- self . __get_label_rotation_and_horizontal_alignment (obj , data , 'x' )
204
+ _get_label_rotation_and_horizontal_alignment (obj , data , 'x' )
204
205
if x_tick_rotation_and_horizontal_alignment :
205
206
self .axis_options .append (x_tick_rotation_and_horizontal_alignment )
206
207
207
208
y_tick_rotation_and_horizontal_alignment = \
208
- self . __get_label_rotation_and_horizontal_alignment (obj , data , 'y' )
209
+ _get_label_rotation_and_horizontal_alignment (obj , data , 'y' )
209
210
if y_tick_rotation_and_horizontal_alignment :
210
211
self .axis_options .append (y_tick_rotation_and_horizontal_alignment )
211
212
212
213
# Set tick position
213
- x_tick_position_string , x_tick_position = \
214
- self .__get_tick_position (obj , data , 'x' )
215
- y_tick_position_string , y_tick_position = \
216
- self .__get_tick_position (obj , data , 'y' )
214
+ x_tick_position_string , x_tick_position = _get_tick_position (obj , 'x' )
215
+ y_tick_position_string , y_tick_position = _get_tick_position (obj , 'y' )
217
216
218
217
if x_tick_position == y_tick_position and x_tick_position is not None :
219
218
self .axis_options .append ("tick pos=%s" % x_tick_position )
@@ -225,6 +224,7 @@ def __init__(self, data, obj):
225
224
# <http://sourceforge.net/p/matplotlib/mailman/message/25169234/>
226
225
# Coordinate of the lines are entirely meaningless, but styles
227
226
# (colors,...) are respected.
227
+ # pylint: disable=protected-access
228
228
if obj .xaxis ._gridOnMajor :
229
229
self .axis_options .append ('xmajorgrids' )
230
230
elif obj .xaxis ._gridOnMinor :
@@ -237,6 +237,7 @@ def __init__(self, data, obj):
237
237
if col != 'black' :
238
238
self .axis_options .append ('x grid style={%s}' % col )
239
239
240
+ # pylint: disable=protected-access
240
241
if obj .yaxis ._gridOnMajor :
241
242
self .axis_options .append ('ymajorgrids' )
242
243
elif obj .yaxis ._gridOnMinor :
@@ -386,128 +387,138 @@ def __init__(self, data, obj):
386
387
387
388
return
388
389
389
- def __get_label_rotation_and_horizontal_alignment (self , obj , data , axes ):
390
- tick_label_text_width = None
391
- tick_label_text_width_identifier = "%s tick label text width" % axes
392
- if tick_label_text_width_identifier in data ['extra axis options' ]:
393
- tick_label_text_width = data ['extra axis options [base]' ][
394
- tick_label_text_width_identifier
395
- ]
396
- del data ['extra axis options' ][tick_label_text_width_identifier ]
390
+ def get_begin_code (self ):
391
+ content = self .content
392
+ if self .axis_options :
393
+ content .append ('[\n ' + ',\n ' .join (self .axis_options ) + '\n ]\n ' )
394
+ return content
395
+
396
+ def get_end_code (self , data ):
397
+ if not self .is_subplot :
398
+ return '\\ end{axis}\n \n '
399
+ elif self .is_subplot and self .nsubplots == self .subplot_index :
400
+ data ['is_in_groupplot_env' ] = False
401
+ return '\\ end{groupplot}\n \n '
402
+
403
+ return ''
397
404
398
- label_style = ""
399
405
400
- major_tick_labels = obj .xaxis .get_majorticklabels () if axes == 'x' \
401
- else obj .yaxis .get_majorticklabels ()
406
+ def _get_label_rotation_and_horizontal_alignment (
407
+ obj , data , axes_obj
408
+ ):
409
+ tick_label_text_width = None
410
+ tick_label_text_width_identifier = \
411
+ '%s tick label text width' % axes_obj
412
+ if tick_label_text_width_identifier in data ['extra axis options' ]:
413
+ tick_label_text_width = data ['extra axis options [base]' ][
414
+ tick_label_text_width_identifier
415
+ ]
416
+ del data ['extra axis options' ][tick_label_text_width_identifier ]
402
417
403
- if len (major_tick_labels ) == 0 :
404
- return None
418
+ label_style = ""
405
419
406
- tick_labels_rotation = \
407
- [ label . get_rotation () for label in major_tick_labels ]
408
- tick_labels_rotation_same_value = len ( set ( tick_labels_rotation )) == 1
420
+ major_tick_labels = \
421
+ obj . xaxis . get_majorticklabels () if axes_obj == 'x' \
422
+ else obj . yaxis . get_majorticklabels ()
409
423
410
- tick_labels_horizontal_alignment = \
411
- [label .get_horizontalalignment () for label in major_tick_labels ]
412
- tick_labels_horizontal_alignment_same_value = \
413
- len (set (tick_labels_horizontal_alignment )) == 1
424
+ if not major_tick_labels :
425
+ return None
414
426
415
- if tick_labels_rotation_same_value and \
416
- tick_labels_horizontal_alignment_same_value :
417
- values = []
427
+ tick_labels_rotation = \
428
+ [ label . get_rotation () for label in major_tick_labels ]
429
+ tick_labels_rotation_same_value = len ( set ( tick_labels_rotation )) == 1
418
430
419
- if any (tick_labels_rotation ) != 0 :
420
- values .append ('rotate=%d' % tick_labels_rotation [0 ])
431
+ tick_labels_horizontal_alignment = \
432
+ [label .get_horizontalalignment () for label in major_tick_labels ]
433
+ tick_labels_horizontal_alignment_same_value = \
434
+ len (set (tick_labels_horizontal_alignment )) == 1
421
435
422
- if tick_label_text_width :
423
- values .append ('align=%s' % tick_labels_horizontal_alignment [0 ])
424
- values .append ('text width=%s' % tick_label_text_width )
425
- else :
426
- print ('Horizontal alignment will be ignored as no \' %s tick '
427
- 'label text width\' has been passed in the \' extra\' '
428
- 'parameter' % axes )
436
+ if tick_labels_rotation_same_value and \
437
+ tick_labels_horizontal_alignment_same_value :
438
+ values = []
439
+
440
+ if any (tick_labels_rotation ) != 0 :
441
+ values .append ('rotate=%d' % tick_labels_rotation [0 ])
429
442
430
- if len ( values ) > 0 :
431
- label_style = \
432
- '%sticklabel style = {%s} ' % ( axes , ',' . join ( values ) )
443
+ if tick_label_text_width :
444
+ values . append ( 'align=%s' % tick_labels_horizontal_alignment [ 0 ])
445
+ values . append ( 'text width=%s ' % tick_label_text_width )
433
446
else :
434
- values = []
447
+ print ('Horizontal alignment will be ignored as no \' %s tick '
448
+ 'label text width\' has been passed in the \' extra\' '
449
+ 'parameter' % axes_obj )
435
450
436
- if tick_labels_rotation_same_value :
437
- values .append ('rotate=%d' % tick_labels_rotation [0 ])
438
- else :
439
- values .append ('rotate={%s,0}[\\ ticknum]'
440
- % ',' .join (str (x ) for x in tick_labels_rotation ))
441
-
442
- if tick_label_text_width :
443
- if tick_labels_horizontal_alignment_same_value :
444
- values .append ('align=%s' %
445
- tick_labels_horizontal_alignment [0 ])
446
- values .append ('text width=%s' % tick_label_text_width )
447
- else :
448
- for idx , x in enumerate (tick_labels_horizontal_alignment ):
449
- label_style += '%s_tick_label_ha_%d/.initial = %s' \
450
- % (axes , idx , x )
451
-
452
- values .append (
453
- 'align=\pgfkeysvalueof{/pgfplots/'
454
- '%s_tick_label_ha_\\ ticknum}' % axes )
455
- values .append ('text width=%s' % tick_label_text_width )
451
+ if values :
452
+ label_style = \
453
+ '%sticklabel style = {%s}' % (axes_obj , ',' .join (values ))
454
+ else :
455
+ values = []
456
+
457
+ if tick_labels_rotation_same_value :
458
+ values .append ('rotate=%d' % tick_labels_rotation [0 ])
459
+ else :
460
+ values .append ('rotate={%s,0}[\\ ticknum]'
461
+ % ',' .join (str (x ) for x in tick_labels_rotation ))
462
+
463
+ if tick_label_text_width :
464
+ if tick_labels_horizontal_alignment_same_value :
465
+ values .append ('align=%s' %
466
+ tick_labels_horizontal_alignment [0 ])
467
+ values .append ('text width=%s' % tick_label_text_width )
456
468
else :
457
- print ( 'Horizontal alignment will be ignored as no \' %s tick '
458
- 'label text width \' has been passed in the \' extra \' '
459
- 'parameter' % axes )
469
+ for idx , x in enumerate ( tick_labels_horizontal_alignment ):
470
+ label_style += '%s_tick_label_ha_%d/.initial = %s' \
471
+ % ( axes_obj , idx , x )
460
472
461
- label_style = 'every %s tick label/.style = {\n ' \
462
- '%s\n ' \
463
- '}' % (axes , ',\n ' .join (values ))
473
+ values .append (
474
+ 'align=\\ pgfkeysvalueof{/pgfplots/'
475
+ '%s_tick_label_ha_\\ ticknum}' % axes_obj
476
+ )
477
+ values .append ('text width=%s' % tick_label_text_width )
478
+ else :
479
+ print (
480
+ 'Horizontal alignment will be ignored as no \' %s tick '
481
+ 'label text width\' has been passed in the \' extra\' '
482
+ 'parameter' % axes_obj
483
+ )
464
484
465
- return label_style
485
+ label_style = 'every %s tick label/.style = {\n ' \
486
+ '%s\n ' \
487
+ '}' % (axes_obj , ',\n ' .join (values ))
466
488
467
- def __get_tick_position (self , obj , data , axes ):
468
- major_ticks = obj .xaxis .majorTicks if axes == 'x' else \
469
- obj .yaxis .majorTicks
489
+ return label_style
470
490
471
- major_ticks_bottom = [tick .tick1On for tick in major_ticks ]
472
- major_ticks_top = [tick .tick2On for tick in major_ticks ]
473
491
474
- major_ticks_bottom_show_all = False
475
- if len ( set ( major_ticks_bottom )) == 1 and major_ticks_bottom [ 0 ] is True :
476
- major_ticks_bottom_show_all = True
492
+ def _get_tick_position ( obj , axes_obj ):
493
+ major_ticks = obj . xaxis . majorTicks if axes_obj == 'x' else \
494
+ obj . yaxis . majorTicks
477
495
478
- major_ticks_top_show_all = False
479
- if len (set (major_ticks_top )) == 1 and major_ticks_top [0 ] is True :
480
- major_ticks_top_show_all = True
496
+ major_ticks_bottom = [tick .tick1On for tick in major_ticks ]
497
+ major_ticks_top = [tick .tick2On for tick in major_ticks ]
481
498
482
- major_ticks_position = None
483
- if not major_ticks_bottom_show_all and not major_ticks_top_show_all :
484
- position_string = "%smajorticks=false" % axes
485
- elif major_ticks_bottom_show_all and major_ticks_top_show_all :
486
- major_ticks_position = 'both'
487
- elif major_ticks_bottom_show_all :
488
- major_ticks_position = 'left'
489
- elif major_ticks_top_show_all :
490
- major_ticks_position = 'right'
499
+ major_ticks_bottom_show_all = False
500
+ if len (set (major_ticks_bottom )) == 1 and major_ticks_bottom [0 ] is True :
501
+ major_ticks_bottom_show_all = True
491
502
492
- if major_ticks_position :
493
- position_string = "%stick pos=%s" % (axes , major_ticks_position )
503
+ major_ticks_top_show_all = False
504
+ if len (set (major_ticks_top )) == 1 and major_ticks_top [0 ] is True :
505
+ major_ticks_top_show_all = True
494
506
495
- return position_string , major_ticks_position
507
+ major_ticks_position = None
508
+ if not major_ticks_bottom_show_all and not major_ticks_top_show_all :
509
+ position_string = "%smajorticks=false" % axes_obj
510
+ elif major_ticks_bottom_show_all and major_ticks_top_show_all :
511
+ major_ticks_position = 'both'
512
+ elif major_ticks_bottom_show_all :
513
+ major_ticks_position = 'left'
514
+ elif major_ticks_top_show_all :
515
+ major_ticks_position = 'right'
496
516
497
- def get_begin_code (self ):
498
- content = self .content
499
- if self .axis_options :
500
- content .append ('[\n ' + ',\n ' .join (self .axis_options ) + '\n ]\n ' )
501
- return content
517
+ if major_ticks_position :
518
+ position_string = \
519
+ "%stick pos=%s" % (axes_obj , major_ticks_position )
502
520
503
- def get_end_code (self , data ):
504
- if not self .is_subplot :
505
- return '\\ end{axis}\n \n '
506
- elif self .is_subplot and self .nsubplots == self .subplot_index :
507
- data ['is_in_groupplot_env' ] = False
508
- return '\\ end{groupplot}\n \n '
509
- else :
510
- return ''
521
+ return position_string , major_ticks_position
511
522
512
523
513
524
def _get_ticks (data , xy , ticks , ticklabels ):
@@ -613,6 +624,7 @@ def _handle_linear_segmented_color_map(cmap):
613
624
# Label the 3 elements in each row in the cdict entry for a given color as
614
625
# (x, y0, y1). Then for values of x between x[i] and x[i+1] the color
615
626
# value is interpolated between y1[i] and y0[i+1].
627
+ # pylint: disable=protected-access
616
628
segdata = cmap ._segmentdata
617
629
red = segdata ['red' ]
618
630
green = segdata ['green' ]
@@ -721,16 +733,16 @@ def _handle_listed_color_map(cmap):
721
733
if cmap .N is None or cmap .N == len (cmap .colors ):
722
734
colors = [
723
735
'rgb(%d%s)=(%.15g,%.15g,%.15g)'
724
- % (k , unit , color [0 ], color [1 ], color [2 ])
725
- for (k , color ) in enumerate (cmap .colors )
736
+ % (k , unit , rgb [0 ], rgb [1 ], rgb [2 ])
737
+ for (k , rgb ) in enumerate (cmap .colors )
726
738
]
727
739
else :
728
740
reps = int (float (cmap .N ) / len (cmap .colors ) - 0.5 ) + 1
729
741
repeated_cols = reps * cmap .colors
730
742
colors = [
731
743
'rgb(%d%s)=(%.15g,%.15g,%.15g)'
732
- % (k , unit , color [0 ], color [1 ], color [2 ])
733
- for (k , color ) in enumerate (repeated_cols [:cmap .N ])
744
+ % (k , unit , rgb [0 ], rgb [1 ], rgb [2 ])
745
+ for (k , rgb ) in enumerate (repeated_cols [:cmap .N ])
734
746
]
735
747
736
748
colormap_string = '{mymap}{[1%s]\n %s\n }' % \
0 commit comments