簡體   English   中英

如何在python中自動化imshow圖

[英]How to automatize imshow plots in python

我想用python3自動化一個有影響力的表現。 我想給出一個數據框,無論給出多少列,這都是繪圖。

我試過這個:

vmin = 3.5
vmax = 6
fig, axes = plt.subplots(len(list(df.columns)),1)

for i,j in zip(list(df.columns),range(1,len(list(df.columns))+1)):

    df = df.sort_values([i], ascending = False) 
    y = df[i].tolist()

    gradient = [y,y]

    plt.imshow(gradient, aspect='auto', cmap=plt.get_cmap('hot_r'), vmin=vmin, vmax=vmax)

    axes = plt.subplot(len(list(df.columns)),1,j)


sm = plt.cm.ScalarMappable(cmap=plt.get_cmap('hot_r'),norm=plt.Normalize(vmin,vmax))
sm._A = []
plt.colorbar(sm,ax=axes)

plt.show()

我的問題是第一組數據(df的第一列)從未顯示過。 地圖也不是我想要的地方。 這正是我得到的:

我的實際輸出

但這就是我想要的:

我想要的輸出

你不應該使用plt.subplot如果您已經通過創建您的次要情節plt.subplots

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

f = lambda x, s: x*np.exp(-x**2/s)/2
df = pd.DataFrame({"A" : f(np.linspace(0,50,600),70)+3.5,
                   "B" : f(np.linspace(0,50,600),110)+3.5,
                   "C" : f(np.linspace(0,50,600),150)+3.5,})

vmin = 3.5
vmax = 6

fig, axes = plt.subplots(len(list(df.columns)),1)

for col, ax in zip(df.columns,axes.flat):

    df = df.sort_values([col], ascending = False) 
    y = df[col].values

    gradient = [y,y]

    im = ax.imshow(gradient, aspect='auto', 
                   cmap=plt.get_cmap('hot_r'), vmin=vmin, vmax=vmax)

# Since all images have the same vmin/vmax, we can take any of them for the colorbar
fig.colorbar(im, ax=axes)

plt.show()

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM