从python字典制作二维密度图

问题描述 投票:-4回答:3

我有一个字典,其中包含每个键的多个值。

dict = {'400.0': [0.051198, 0.051198, 0.045406, 0.15855, 0.33586, 0.079011, 0.085342], '420.0': [0.046287, 0.046287, 0.0061685, 0.099437, 0.27868, 0.093667, 0.093032], '440.0': [0.046454, 0.046454, 0.0015765, 0.052886, 0.23238, 0.084899, 0.088889], '460.0': [0.041944, 0.041944, 0.0083426, 0.027038, 0.19666, 0.032842, 0.07963], '480.0': [0.025476, 0.025476, 0.021437, 0.02845, 0.16916, 0.066085, 0.11551], '500.0': [0.037221, 0.037221, 0.02546, 0.01049, 0.14636, 0.15647, 0.17047], '520.0': [0.020769, 0.020769, 0.057708, 0.013889, 0.11284, 0.17052, 0.15862], '540.0': [0.0029368, 0.0029368, 0.086186, 0.026225, 0.090237, 0.098873, 0.072785], '560.0': [0.0024553, 0.0024553, 0.1034, 0.057088, 0.077615, 0.048153, 0.037146], '580.0': [0.020523, 0.020523, 0.105, 0.076162, 0.067578, 0.047222, 0.062943], '600.0': [0.042838, 0.042838, 0.09883, 0.077351, 0.056873, 0.048235, 0.096264], '620.0': [0.05615, 0.05615, 0.096128, 0.073473, 0.046765, 0.031932, 0.10801], '640.0': [0.065999, 0.065999, 0.093658, 0.088445, 0.040698, 0.0082049, 0.095002], '660.0': [0.099263, 0.099263, 0.074047, 0.13282, 0.041967, 0.016028, 0.1117], '680.0': [0.14634, 0.14634, 0.028374, 0.19581, 0.071541, 0.036822, 0.14719], '700.0': [0.13285, 0.13285, 0.023217, 0.23739, 0.0053786, 0.059918, 0.17545]}

键是“x”值,字典值是“y”值。

现在我想制作密度图。这可能与matplotlib,如果是,如何?非常感谢你。

编辑:

我认为plt.imshow是个不错的选择

python dictionary plot
3个回答
2
投票

这将完成我的工作。请检查并让我知道。谢谢。

import pandas as pd
import matplotlib.pyplot as plt


dict1 = {
        '400.0': [0.051198, 0.051198, 0.045406, 0.15855, 0.33586, 0.079011, 0.085342],
        '420.0': [0.046287, 0.046287, 0.0061685, 0.099437, 0.27868, 0.093667, 0.093032],
        '440.0': [0.046454, 0.046454, 0.0015765, 0.052886, 0.23238, 0.084899, 0.088889],
        '460.0': [0.041944, 0.041944, 0.0083426, 0.027038, 0.19666, 0.032842, 0.07963],
        '480.0': [0.025476, 0.025476, 0.021437, 0.02845, 0.16916, 0.066085, 0.11551],
        '500.0': [0.037221, 0.037221, 0.02546, 0.01049, 0.14636, 0.15647, 0.17047],
        '520.0': [0.020769, 0.020769, 0.057708, 0.013889, 0.11284, 0.17052, 0.15862],
        '540.0': [0.0029368, 0.0029368, 0.086186, 0.026225, 0.090237, 0.098873, 0.072785],
        '560.0': [0.0024553, 0.0024553, 0.1034, 0.057088, 0.077615, 0.048153, 0.037146],
        '580.0': [0.020523, 0.020523, 0.105, 0.076162, 0.067578, 0.047222, 0.062943],
        '600.0': [0.042838, 0.042838, 0.09883, 0.077351, 0.056873, 0.048235, 0.096264],
        '620.0': [0.05615, 0.05615, 0.096128, 0.073473, 0.046765, 0.031932, 0.10801],
        '640.0': [0.065999, 0.065999, 0.093658, 0.088445, 0.040698, 0.0082049, 0.095002],
        '660.0': [0.099263, 0.099263, 0.074047, 0.13282, 0.041967, 0.016028, 0.1117],
        '680.0': [0.14634, 0.14634, 0.028374, 0.19581, 0.071541, 0.036822, 0.14719],
        '700.0': [0.13285, 0.13285, 0.023217, 0.23739, 0.0053786, 0.059918, 0.17545]}

df = pd.DataFrame(dict1)

df.plot.density()
plt.show()

1
投票

如果要沿x轴使用键和沿y轴使用值,您可以:

import matplotlib.pylab as plt

fig=plt.figure();
ax=fig.add_subplot(111);
ax.plot(dict.keys(),dict.values(),marker='.',ms=10,linestyle='none');
plt.show();

然而,这只是散点图。密度图是什么意思?


1
投票

这是你想要的吗 ?

import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(dict1)
z=df.values


fig, ax = plt.subplots(figsize=(6,6))
plt.imshow(z,
           origin='lower', aspect='auto',
           extent=[0, 3, 0, 1])
plt.colorbar();

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.