Search code examples
pythonpandasdataframeplotxticks

How do I add tick labels to a pandas plot?


I have a pandas data frame that I have graphed in a plot but for some reason I can't get my plot to have xtick labels. I have tried using matplotlib but it doesn't seem to work. Here is what I have for the plot.

 wsdf = pd.DataFrame(list(wshots.items()),columns = ['Coordinates', 'Occurences (S, G)'])
    wsdf[['S','G']] = pd.DataFrame(wsdf['Occurences (S, G)'].tolist(), index=wsdf.index) 
    wsdf[['X-','Y']] = pd.DataFrame(wsdf['Coordinates'].tolist(), index=wsdf.index)
    wsdf['wShot Percentage'] = wsdf['G'] / wsdf['S']
    wsdf = wsdf.drop(['Coordinates', 'Occurences (S, G)'], axis = 1)
    wsdf['X'] = abs(wsdf['X-'])
    wsdf = wsdf.drop(['X-'], axis = 1)
    wsdf = wsdf[["X", "Y", "S", "G", "wShot Percentage"]]
#makes it so there aren't any coordinates that dont have more than x shots
    print(wsdf)
    wsdf = wsdf.loc[wsdf['S'] >=1]

# scatter plot 
    wshot_plot = wsdf.plot.scatter(
                          x='X',
                          y='Y',
                          c='wShot Percentage',
                          colormap='RdYlGn',
                          alpha = 1/3)

This is what I tried to get the xticks

xtick = [50, 60, 70, 80, 90, 100]
 
    plt.plot(x, y)
    plt.xlabel('xtick')
 
    plt.show()

Anything will help! Thanks!


Solution

  • I think you're wanting to use xticks or xticklabels.

    import matplotlib.pyplot as plt
    
    x_vals = [55,65,75]
    y_vals = [30,40,50]
    fig, ax = plt.subplots()
    ax.scatter(x=x_vals,
               y=y_vals)
    xtick = [50, 60, 70, 80, 90, 100]
    ax.set_xticks(xtick) #specify where you want your ticks
    # If you want the labels to be something other than the default x_tick values.
    # ax.set_xticklabels(["A", "B", "C", "D", "E"]) 
    
    plt.show()