#!/usr/bin/env python # coding: utf-8 # # Rotation of axis labels # # The `angle` parameter in `element_text()` function allows to rotate the text. This functionality is currently available to rotate axis labels (i.e. for the `axis_text, axis_text_x, axis_text_y` parameters in a `theme()`). # 1. [Default axis labels layout](#1.-Default-Axis-Labels-Layout) # # 2. [Layout labels for the discrete axis](#2.-Layout-labels-for-the-discrete-axis) # # 3. [Layout labels for continuous axis](#3.-Layout-labels-for-continuous-axis) # In[1]: import pandas as pd from lets_plot import * # In[2]: LetsPlot.setup_html() LetsPlot.set_theme(theme_light()) # In[3]: df = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/midwest.csv') df.head(3) # In[4]: df['state'].unique() # In[5]: df['state'] = df['state'].map( {'IL': 'Illinois', 'IN':'Indiana', 'MI':'Michigan', 'OH': 'Ohio', 'WI' : 'Wisconsin'} ) # In[6]: def plot_matrix(plots=[], width=400, height=300, columns=2): bunch = GGBunch() for i in range(len(plots)): row = int(i / columns) column = i % columns bunch.add_plot(plots[i], column * width, row * height, width, height) return bunch.show() angles = [0, 45, 90, -10] highlight_axis_labels = theme( text=element_text(color='gray', size=13), axis_text=element_text(color='blue') ) def plot_with_text_angles(base_plot, text_elem_name): plots = [] for angle in angles: plots.append( base_plot + ggtitle("angle={0}".format(angle)) + theme(**{text_elem_name: element_text(angle=angle)}) ) return plot_matrix(plots) # #### 1. Default Axis Labels Layout # In[7]: p = ggplot(df) + geom_jitter(aes('state', 'poptotal'), color='light_gray') + scale_y_log10() p # #### 2. Layout labels for the discrete axis # Let's change rotation angle to check placement of labels on a discrete axis. Labels for the continuous will be disabled to avoid cluttering the plot. # ##### Horizontal axis # In[8]: plot_with_text_angles( p + theme(axis_text_y='blank') + highlight_axis_labels, 'axis_text_x' ) # In[9]: # Change axis position plot_with_text_angles( p + theme(axis_text_y='blank') + highlight_axis_labels + scale_x_discrete(position="top"), 'axis_text_x' ) # ##### Vertical axis # When the rotation angle is set to 90°, some labels are not displayed to avoid overlapping. # In[10]: plot_with_text_angles( p + theme(axis_text_y='blank') + highlight_axis_labels + coord_flip(), 'axis_text_x' ) # In[11]: # Change axis position plot_with_text_angles( p + theme(axis_text_y='blank') + highlight_axis_labels + scale_x_discrete(position="top") + coord_flip(), 'axis_text_x' ) # #### 3. Layout labels for continuous axis # For continuous axis the number of labels varies depending on how many it can fit. # ##### Horizontal axis # Changing the angle of rotation changes the number of labels on the axis: # 90° rotation allows to place more labels on the horizontal axis. # In[12]: plot_with_text_angles( p + theme(axis_text_x='blank') + highlight_axis_labels + coord_flip(), 'axis_text_y' ) # In[13]: # Change axis position plot_with_text_angles( p + theme(axis_text_x='blank') + highlight_axis_labels + coord_flip() + scale_y_log10(position='right'), 'axis_text_y' ) # ##### Vertical axis # By changing the angle of rotation, thereby increasing the label heights, we get a reduction in the total number of labels on the vertical axis. # In[14]: plot_with_text_angles( p + theme(axis_text_x='blank') + highlight_axis_labels, 'axis_text_y' ) # In[15]: # Change axis position plot_with_text_angles( p + theme(axis_text_x='blank') + highlight_axis_labels + scale_y_log10(position='right'), 'axis_text_y' )