Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 情节:如何修复树状图留下的巨大空白?_Python_Plotly_Plotly Python - Fatal编程技术网

Python 情节:如何修复树状图留下的巨大空白?

Python 情节:如何修复树状图留下的巨大空白?,python,plotly,plotly-python,Python,Plotly,Plotly Python,我正在研究这个函数,它创建了一个树状图。dataframe可能相当大,因此我想根据其内容调整图的大小。但对于较大的数字,y形记号的分布并不均匀,最终留下了这个丑陋的大空白 df = pd.DataFrame( np.random.random(size=(1000, 1000)) ) def plotly_dendrogram(df: pd.DataFrame(), labels=None, orientation='left', color_th

我正在研究这个函数,它创建了一个树状图。dataframe可能相当大,因此我想根据其内容调整图的大小。但对于较大的数字,y形记号的分布并不均匀,最终留下了这个丑陋的大空白

df = pd.DataFrame( np.random.random(size=(1000, 1000)) )

def plotly_dendrogram(df: pd.DataFrame(), labels=None, 
                      orientation='left', color_threshold=0,
                      height=None, width=None, max_label_lenght=30):
    if labels is None:
        labels = df.index
        
    if height is None:
        height = max(300, 10*len(df))
    fig = ff.create_dendrogram(df, color_threshold=color_threshold, 
                               labels=labels, orientation=orientation)
    fig.update_layout(width=width, height=height, font_family="Monospace")
    fig.update_layout(xaxis_showgrid=True, yaxis_showgrid=True)
    fig.update_yaxes(automargin=True)
    fig.update_xaxes(automargin=True)
    return fig

plotly_dendrogram(df)
有没有办法使刻度在可用空间上均匀分布

即使是最新的plotly版本,这似乎也是一个明显存在的问题。对于较大的数据集,背景似乎与树状图本身越来越不一致。但事实证明,通过检索所有记录道的全局最小值和最大值并相应地设置y轴范围,可以很容易地解决此问题,如下所示:

y_max = []
y_min = []
for t in fig['data']:
    y_max.append(t['y'].max())
    y_min.append(t['y'].min())

rngs = [min(y_min), max(y_max)]
fig.update_layout(yaxis=dict(range=[rngs[0]-5, rngs[1]]))
fig.show()

完整代码:
我的建议对你效果如何?
import plotly.figure_factory as ff
import pandas as pd
import numpy as np

# data sample
np.random.seed(123)
df = pd.DataFrame( np.random.random(size=(500, 500)))
df.index = df.index.map(str)

def plotly_dendrogram(df: pd.DataFrame(), labels=None, 
                      orientation='left', color_threshold=0,
                      height=None, width=None, max_label_lenght=30):
    if labels is None:
        labels = df.index
    
    if max_label_lenght is not None:
        labels = [i[:max_label_lenght] for i in labels]
        
    if height is None:
        height = max(300, 10*len(df))
    fig = ff.create_dendrogram(df, color_threshold=color_threshold, 
                               labels=labels, orientation=orientation)
    fig.update_layout(width=width, height=height, font_family="Monospace")
    fig.update_layout(xaxis_showgrid=True, yaxis_showgrid=True)
    fig.update_yaxes(automargin=True)
    fig.update_xaxes(automargin=True)
    return fig

fig = plotly_dendrogram(df)

y_max = []
y_min = []
for t in fig['data']:
    y_max.append(t['y'].max())
    y_min.append(t['y'].min())

fig.update_layout(yaxis=dict(range=[min(y_min)-5, max(y_max)]))
fig.show()