调整scipy树形图和matplotlib pcolor子图,同时保持对齐

时间:2017-04-16 18:07:19

标签: python matplotlib scipy heatmap dendrogram

目标

将树形图连接到热像图,就像matlab制作的那样;

enter image description here

或者像this plotly example一样没有花哨的javascript只使用scipy和matplotlib。

建立this question我得出以下代码;

INPUT

from scipy.cluster.hierarchy import linkage
from scipy.cluster.hierarchy import dendrogram
from scipy.spatial.distance import pdist
import matplotlib
from matplotlib import pyplot as plt
import numpy as np
from numpy import arange

# Create the figure and set it's size.
fig = plt.figure(figsize=(5,7))

# Create the first subplot in the figure. 
ax1 = plt.subplot(211)

# Set to your favorite colormap.
cm = matplotlib.cm.viridis

# Create array of random numbers.
X = np.random.random([6,300])

# Create a linkage object.
linkmat = linkage(X)

# Make a dendrogram from the linkage object.
dendrogram(linkmat)

# Use the x and y limits to set the aspect.
x0,x1 = ax1.get_xlim()
y0,y1 = ax1.get_ylim()
ax1.set_aspect((x1-x0)/(y1-y0))


# Remove the ticks on the x-axis. 
plt.tick_params(axis='x', which='both', bottom='off', top='off', labelbottom='off')

# Create the second subplot.
ax2 = plt.subplot(212)

labels = ["a", "b", "c", "d", "e"]

plt.xticks(arange(0.5, 7.5, 1))

plt.gca().set_xticklabels(labels)

plt.pcolor(X.T)

x0,x1 = ax2.get_xlim()
y0,y1 = ax2.get_ylim()

ax2.set_aspect((x1-x0)/(y1-y0))

# Insert the color scale
plt.colorbar()
cb = plt.colorbar(ax=ax1)
cb.ax.set_visible(False)

# Make the vertical distance between plots equal to zero 
plt.subplots_adjust(hspace=0)

# Show the plot
plt.show()

输出

enter image description here

问题

如何在保持当前对齐的同时将树形图的高度减少 50%并将热图的高度增加 50%?

1 个答案:

答案 0 :(得分:1)

您可以使用 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:b="http://bootsfaces.net/ui" xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"> <h:head> <title>Add data</title> <h:outputScript library="js" name="jquery-3.2.1.min.js" /> </h:head> <h:body> <h:form> <b:commandButton look="link" action="response?faces-redirect=true" value="Response" /> <b:commandButton look="link" action="fields?faces-redirect=true" value="Fields" /> </h:form> <style> .preformatted { white-space: pre; } </style> <h:outputText class="preformatted" escape="false" value="#{parserBean.parseToHtml()}" /> </h:body> </html> 代替subplot2grid({params}),因为您可以知道它将占用多少网格

subplot()

enter image description here