热图作为matplotlib中图的背景

时间:2018-09-23 21:18:55

标签: python python-2.7 matplotlib plot heatmap

我有以下情节:

figure1

已使用以下代码生成:

h1 Word Of The Day
p= wordOfTheDay
br
h1 Word List
ul
  each word in words
    li= word.keyword

所以我的实际问题是:如何添加风险热图作为背景(与此相同: https://www.latestquality.com/risk-heat-map/

我已经尝试的东西:

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

ttc = pd.read_excel("../XLSX_Files/Baseline_Data.xlsx", sheet_name="Data")["TTC"]
risks = col.OrderedDict(
[("very low", 0), ("low", 0), ("medium-to-low", 0), ("medium", 0), ("high-to-medium", 0), ("high", 0),
 ("very high", 0)])

---- Part in which risks get filled --- 

for x in risks:
     risk_distribution.append(float(risks[x]) / len(ttc))

x = np.arange(1, len(risks) + 1)
y = np.arange(0.0, 0.4, 0.05)
xticks = risks.keys()
fig, ax = plt.subplots()
plt.plot(x, risk_distribution)
ax.set_ylim(0, 0.4)
plt.show()

但这给了我一些奇怪的图形。 enter image description here

是否有可能在不丢失y维的情况下添加热图?

1 个答案:

答案 0 :(得分:1)

您遇到的问题是y刻度不一定匹配。您需要叠加2个共享相同x轴的轴。您可以使用#include <stdio.h> typedef struct distance{ float feet; float inches; } dist; struct addDistance(struct distance dist1, struct distance dist2){ struct dist3; dist3.feet = dist1.feet + dist2.feet; dist3.inches = dist1.inches + dist2.inches; return dist3; } int main(){ dist dist1; dist dist2; dist1.feet = 2; dist2.feet = 3; dist1.inches = 6; dist2.inches = 4; addDistance(dist1, dist2); } 完成此操作。

matplotlib.axes.Axes.twinx

这里是另一个example