Git Product home page Git Product logo

Comments (5)

mwaskom avatar mwaskom commented on June 3, 2024

Hi, this isn't a reproducible example so it's really impossible to say. The colorbar on a heatmap will "steal" space from the axes with the heatmap by default, maybe that's what you're seeing.

I'm going to close but can reopen with a reproducible example that demonstrates an issue in seaborn.

from seaborn.

Niroznak avatar Niroznak commented on June 3, 2024

Hi,

i am aware that the color bar steals space, but in the image i uploaded i removed the axes (the pareto bar also contains second axis) its also taking space, with the axes its even more significant.
this is how it looks like with axes:

Screenshot 2024-02-21 205755

here is the data i used for the plotting
test_data.zip

and the code i used for it:

`
def plot_heatmap_to_ax(data,ax,title=None,annot=False,mark_max=True,x_items=10,y_items=10):
import seaborn as sns
assert len(data.columns) == 3,print('only 3 columns dataframe: columns,rows,values')
(x1,x2,y)=data.columns
plot_df = data.pivot(index=x1, columns=x2, values=y)
plot_df = plot_df.loc[plot_df.sum(axis=1).sort_values(ascending=False).index[:y_items], plot_df.sum(axis=0).sort_values(ascending=False).index[:x_items]]
cmap = 'OrRd' # 'RdYlGn_r'.
p = sns.heatmap(data=plot_df, ax=ax, xticklabels=1, cmap=cmap, robust=True, annot=annot, yticklabels=True)
ax.title.set_text(title)
if mark_max:
from matplotlib.patches import Rectangle
text_color = 'midnightblue' # 'navy'
mark_text = 'Max '
ax.text(0, len(plot_df), f'{mark_text} {x2}', color=text_color, weight='bold', rotation=90,size=10, ha='left') # , fontweight='bold'
ax.text(0, 0, f'{mark_text} {x1}', color=text_color, weight='bold', rotation=0, size=10,va='top') # 'baseline', 'center_baseline'
ax.add_patch(Rectangle((0, 0), 1, plot_df.shape[0], linestyle='--', fill=False, edgecolor='red'))
ax.add_patch(Rectangle((0, 0), plot_df.shape[1], 1, linestyle='--', fill=False, edgecolor='red'))
return ax

def plot_pareto_to_ax(data,ax,x,y1,y2,axis_labels,annot=False,title=None):
if not isinstance(y1, list):
y1 = [y1]
labels = y1.copy()
if y2 is not None:
if not isinstance(y2, list):
y2 = [y2]
labels += y2

data[x] = data[x].astype(str)
p1 = ax.plot(data[x], data[y1], label=y1)#, color=colors[l], marker='o')
if annot:
    for j in y1:
        for k in range(len(data)):
            ax.annotate(data[j][k], (data[x][k], data[j][k]))#, fontweight='light')
ax.set_xlabel(axis_labels[0])
ax.set_ylabel(axis_labels[1])
ax.grid(axis='y')
if y2 is not None:
    ax2 = ax.twinx()
    # set color cycle to next color
    for advance in range(len(y1)):
        ax2._get_lines.get_next_color()
    p2 = ax2.plot(data[x], data[y2], label=y2,linestyle='None')
    if annot:
        for j in y2:
            for k in range(len(data)):
                ax2.annotate(data[j][k], (data[x][k], data[j][k]))#, fontweight='light')
    ax2.set_ylabel(axis_labels[2])
# set joined legend
lns = p1+p2
ax.legend(lns, labels, loc=1)
ax.set_xticklabels(data[x], rotation=90)
ax.title.set_text(title)
return ax

fig, axes = plt.subplots(ncols=1, nrows=2 , figsize=(7,7))
axes[0] = plot_heatmap_to_ax(data,ax=axes[0],title='Test', annot=False, mark_max=False, x_items=30, y_items=30)
axes[1] = plot_pareto_to_ax(data=Fleet_summary,ax=axes[1], x='x', y1=['y1', 'y1-2'], y2='y2',axis_labels=['x', '% y', '# y2'], title='')
`

from seaborn.

mwaskom avatar mwaskom commented on June 3, 2024

To demonstrate that this is a problem in seaborn, you'll need to clean up your example and make it much simpler, and show that it doesn't happen if you use matplotlib directly.

from seaborn.

Niroznak avatar Niroznak commented on June 3, 2024

i think this example is very simple to show the matplotlib has no right padding as the seaborn, you can easily add more matplotlib plots by duplicating axes[1] multiple times, create the exact same plot (or is you prefer create different type of plot), and change the number of rows accordingly, you will see that all matplotlib plots will have same width except the seaborn plot.

n=5 fig, axes = plt.subplots(ncols=1, nrows=n , figsize=(7,7)) axes[0] = plot_heatmap_to_ax(data,ax=axes[0],title='Test', annot=False, mark_max=False, x_items=30, y_items=30) for i in range(1,5): axes[i] = plot_pareto_to_ax(data=Fleet_summary,ax=axes[i], x='x', y1=['y1', 'y1-2'], y2='y2',axis_labels=['x', '% y', '# y2'], title='')

you can adjust the position, seaborn to be first/last/in the middle and see where this comes from, etc...

BR,
Nir

from seaborn.

Niroznak avatar Niroznak commented on June 3, 2024

Hi, i resolved it, it was the layout setting in the figure creation, adding layout="constrained" to figure forces all axes to same width, you can close it.

Thanks,
Nir

from seaborn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.