简体   繁体   中英

Plot list as colored squares matplotlib

I would like to plot an array of lists xs in the same vein as a heatmap. The difference is that the result has to be projected on an actual ys axis.

xs = [[0, 100.0, 0, 100.0, 0, 100.0, 0, 100.0], [0, 100.0, 0, 100.0, 0, 100.0, 0, 100.0], ...]

ys = [0.00000e+00 0.00000e+00 4.90000e-02 -4.00000e-03 4.30000e-02 2.00000e-02 -8.00000e-03 -1.58000e-01...]

Each value of an xs list should be represented as a different colored square depending on itsvalue (light blue for 0 and dark blue for 100 for example). If two xs points have the same ys , the most recent value has to be plotted.

I am trying to plot the result on a matplotlib subplot that is using ys as y-axis. I have tried with a seaborn heatmap and the visual result was exactly what I wanted, but I did not manage to use ys as my y-axis. 使用 seaborn 热图绘制结果

Edit: Here is an example of what I have and what I would like to get.

例子

What I'm using now is: sns.heatmap(xs,cbar=False,ax=axs[col]) with axs[col] being the subplot where my "heatmap" is supposed to be drawn.

If you consider using a pandas DataFrame, then you could take advantage of drop_duplicates() to keep only one of the row for each y-value. Then it's just a matter of calling sns.heatmap() on the filtered dataframe:

ys = [150,150,151,152,153,153,154]
xs = np.random.choice([0,50,100], size=(len(ys),8))
df = pd.DataFrame(xs, index=ys)
df['ys'] = ys
df2 = df.drop_duplicates(subset='ys', keep='last').set_index('ys')
sns.heatmap(df2, cmap='Blues')

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM