Home > database >  Multiple lollipop plots in a for loop in Matplotlib: Setting xticks and removing spines
Multiple lollipop plots in a for loop in Matplotlib: Setting xticks and removing spines

Time:01-25

I have the following data that I want to use to generate multiple lollipop plots in Matplotlib using a for loop. I have been partially successful in doing that. There are couple of areas I want some help with:

  1. setting the plt.xticks to update based on the data. I have set a place holder commenting out that part in the code below.
  2. how to turn of the spines for the top, right, and bottom in the figure. I also have a placeholder, commented out in the code below.

Below is that data I want to plot

import pandas as pd
import matplotlib.pyplot as plt

data = {'v1': [24.57, 13.91, 13.72, 22.27, 8.90, 4.77, 6.72],
        'v2': [4.8, 5.61, 18.96, 4.66, 22.53, 28.78, 20.15],
        'v3': [17.31, 15.73, 14.62, 14.19, 13.83, 13.36, 10.96]
        }

data2 = pd.DataFrame(data, index=['V{}'.format(x) for x in range(0, 7)])
print(data2)

And the code I am using to plot the data

for elem in data2:
    data2 = data2.sort_values(by=[elem], ascending=True)
    rng = range(1,len(data2.index) 1)
    
    annotations = (data2[elem].values.tolist())
    
    fig = plt.figure(figsize=(8, 5))
    
    plt.hlines(y=rng, xmin=0, xmax=data2[elem], color='skyblue')
    plt.plot(data2[elem], rng, "o")
    plt.yticks(rng, data2.index, color='dimgrey')
#   plt.xticks()
    plt.tick_params(top=False, left=False, bottom=False, right=False, labelleft=True, labelbottom=True,
               labelcolor='dimgrey')
    plt.title("output - {}".format(elem), loc='center', color='dimgrey')
    plt.xlabel('XD', color='dimgrey')
    plt.ylabel('YD', color='dimgrey')
#   [plt.spines[loc].set_visible(False) for loc in ['right', 'top', 'bottom']] # turn off spines
    for i, label in enumerate(annotations):
        plt.annotate(label, (data2[elem][i] 0.45, rng[i]),
                    color='dimgrey')
    plt.show()

CodePudding user response:

  1. You have to define the name and the locations of the x-ticks. I don't fully understand the data that is being placed; however, you can just change the val__x_ticks to the locations where you want the ticks to be and the tick_names to the names of your desired ticks.

  2. To turnoff the spines of your axis, you have to use the Axis method. I have changed the code a bit to use subplots, that I can get the axis method.

Please find attached below my version of the code, that I think answers both your questions.


data = {'v1': [24.57, 13.91, 13.72, 22.27, 8.90, 4.77, 6.72],
        'v2': [4.8, 5.61, 18.96, 4.66, 22.53, 28.78, 20.15],
        'v3': [17.31, 15.73, 14.62, 14.19, 13.83, 13.36, 10.96]
        }

data2 = pd.DataFrame(data, index=['V{}'.format(x) for x in range(0, 7)])
print(data2)

num_x_ticks = 5
val_x_ticks = [0, 7, 10, 12, 30]
tick_names = ['0', '7', '10', '12', '30']

for elem in data2:
    data2 = data2.sort_values(by=[elem], ascending=True)
    
    rng = range(1,len(data2.index) 1)
    
    annotations = (data2[elem].values.tolist())
    
    ax = plt.subplot(111)

    ax.hlines(y=rng, xmin=0, xmax=data2[elem], color='skyblue')
    ax.plot(data2[elem], rng, "o")
    ax.set_yticks(rng, data2.index, color='dimgrey')
    plt.xticks(val_x_ticks, tick_names)
    ax.tick_params(top=False, left=False, bottom=False, right=False, labelleft=True, labelbottom=True,
               labelcolor='dimgrey')
    ax.set_title("output - {}".format(elem), loc='center', color='dimgrey')
    ax.set_xlabel('XD', color='dimgrey')
    ax.set_ylabel('YD', color='dimgrey')
    [ax.spines[loc].set_visible(False) for loc in ['right', 'top', 'bottom']] 
    for i, label in enumerate(annotations):
        plt.annotate(label, (data2[elem][i] 0.45, rng[i]),
                    color='dimgrey')
    plt.show()
  •  Tags:  
  • Related