Search code examples
pythonpython-3.xpandasscatter-plot

Pandas - scatter plot - rotation of cmap label


I am doing a simple scatter plot directly via pandas.

My data frame has columns A,B,C.

I plot A vs. B and visualize C values via color.

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    
    %matplotlib inline

Code:

    ycol_label = 'B'
    xcol_label = 'A'
    
    f = df1.plot.scatter(x=xcol_label, y=ycol_label, c='C', cmap='coolwarm')
    
    h = plt.ylabel(ycol_label)
    h.set_rotation(0)
    type(f)

How do I change the orientation of that C label which I have on the far right?

I want to rotate it and show it as the B label is shown (on the far left).

I have hard time to even google for this because I don't know how this rightmost element
is called (the one with the cool warm color scale). I want to rotate that element's label basically.

pic9


Solution

  • I don't know how to do it using pandas plotting, but with a normal matplotlib plotting it works like this:

    x = np.random.random(50)
    y = np.random.random(50)
    c = np.arange(50)
    fig, ax = plt.subplots()
    f = ax.scatter(x, y, c=c, cmap='coolwarm')
    cbar = plt.colorbar(f)
    
    ax.set_xlabel('x axis')
    ax.set_ylabel('y axis', rotation=0)
    cbar.set_label('z axis', rotation=0)
    

    Output: enter image description here

    ---edit---

    This is pandas plotting version:

    x = np.random.random(50)
    y = np.random.random(50)
    c = np.arange(50)
    df1 = pd.DataFrame({'a': x, 'b': y, 'c': c})
    ycol_label = 'b'
    xcol_label = 'a'
    
    f = df1.plot.scatter(x=xcol_label, y=ycol_label, c='c', cmap='coolwarm')
    
    ff = plt.gcf()
    cax = ff.get_axes()[1]
    cax.set_ylabel('test', rotation=0)
    
    h = plt.ylabel(ycol_label)
    h.set_rotation(0)
    
    type(f)
    

    Output:

    enter image description here