如何製作 Pandas DataFrame 的簡單等高線圖,其中數字單元格值為 Z,行/列標記為 X 和 Y 坐標?

克..

我有一個 Pandas DataFrame,luminance_df看起來像這樣:

隱約可見 超輕 升位之光 中等的 變暗 黑暗的 更暗的 超暗 幾乎是黑色的
橘子 96 92 83 72 61 53 48 40 34 28
金子 96 89 77 65 56 50 44 37 31 26
黃色的 95 88 77 64 53 47 40 33 29 26
黃綠色 95 89 80 67 55 44 35 27 23 20
綠色 97 93 85 73 58 45 36 29 24 20
森林 96 90 80 67 52 39 30 24 20 16
水色 97 89 78 64 50 40 32 26 22 19
青色 96 90 82 69 53 43 36 31 27 24
淺藍 97 94 86 74 60 48 39 32 27 24
藍色 97 93 87 78 68 60 53 48 40 33
靛青 97 94 89 82 74 67 59 51 41 34
紫色的 98 95 92 85 76 66 58 50 42 35
皇家紫 98 95 92 85 75 65 56 47 39 32
品紅 98 95 91 83 73 61 49 40 33 28
粉色的 97 95 90 82 70 60 51 42 35 30
粉紅色 97 95 90 82 71 60 50 41 35 30
紅色的 97 94 89 82 71 60 51 42 35 31

到目前為止,我正在構建一個多圖表 HTML 文件,如下所示:

with open(os.path.join(cwd, 'testout.html'), 'w') as outfile:
    outfile.write("<p>&nbsp;</p><hr/><p>&nbsp;</p>".join(['<h1>Colors</h1>'+hex_styler.to_html(), '<h1>Hue</h1>'+hue_styler.to_html(), '<h1>Saturation</h1>'+saturation_styler.to_html(
    ), '<h1>Luminance</h1>'+luminance_styler.to_html(), '<h1>Perceived Brightness</h1>'+perceived_brightness_pivot_styler.to_html(), '<h1>Base Data</h1>'+basic_df.to_html()]))

我想在 之後立即顯示亮度的高程/輪廓樣式圖,luminance_styler.to_html()很像我在 Excel 中製作的這張圖:

來自 Excel 的亮度等高線圖

我希望顏色保持“從上到下”排序為 y 軸上的值,而暗度保持“從左到右”排序為 x 軸上的值,就像上面的例子一樣。

問題

我不是數據科學家,也不經常使用 Python。我為自己一開始就製作而感到自豪luminance_df,但在我的一生中,我並沒有弄清楚如何簡單地製作 Python ......處理 DataFrame 中的數字單元格值,其兩個方向的標籤都是 strings 。 .. 作為 z 軸並製作它的等高線圖。

我谷歌的一切都會導致非常複雜的數據科學細微問題。

Could someone get me on the right track by giving me the basic "hello world" code to get at least as far with luminance_df's data in Python as I got with the "insert chart" button in Excel?

If you can get me so I've got a img = BytesIO() that's image_base64 = base64.b64encode(img.read()).decode("utf-8")-able, I can f'<img src="data:image/png;base64, {image_base64}" />' it myself into the string concatenation that makes testout.html.

I'm on Windows and have myself set up to be able to pip install.

Notes

  1. To be fair, I find these contour charts much more attractive and much easier to read than the one Excel made, but I'm fine with something sort of "brutish"-looking like the Excel version, as long as it makes "rising" & "falling" obvious and as long as it uses a ROYIGBV rainbow to indicate "less" vs. "more" (pet peeve of mine about the default Excel colors -- yes, I know, it's probably an accessibility thing):

    彩虹等高線圖

    另一個彩虹等高線圖

    又一個彩虹等高線圖

  2. While I'd like my chart's colors to follow a "rainbow" of sorts (because personally I find them easy to read), any "rainbow shading" on the chart should completely ignore the fact that the labels of the y-axis happen to describe colors. No correlation whatsoever. I'm simply plotting number facts between 16 and 98; colors of the chart should just indicate the change in "elevation" between those two extremes.

Effort so far

The only other "simple" question I've found so far that seems similar is Convert pandas DataFrame to a 3d graph using Index and Columns as X,Y and values as Z?, but this code didn't work for me at all, so I don't even know what it outputs, visually, so I have no idea if it's even relevant:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
lumX = luminance_df.columns
lumY = luminance_df.index
lumZ = luminance_df.values
fig = plt.figure()
ax = plt.axes(projection = '3d')
ax.contour3D(lumX,lumY,lumZ)

我的腳本出錯並顯示一條消息:ValueError: could not convert string to float: 'orange',我不知道我在做什麼足以適應這個答案似乎是圍繞數字 X 軸和 Y 軸鍵的假設編寫的。(此外,它可能不會生成我希望的圖表類型——正如我所說,無法判斷,因為它甚至不執行,而且答案中沒有視覺樣本。)

數據集

準備pandas.DataFrame()

{"barelyvisible":{"orange":96,"gold":96,"yellow":95,"chartreuse":95,"green":97,"forest":96,"aqua":97,"teal":96,"lightblue":97,"blue":97,"indigo":97,"purple":98,"royalpurple":98,"magenta":98,"pink":97,"dustypink":97,"red":97},"ultralight":{"orange":92,"gold":89,"yellow":88,"chartreuse":89,"green":93,"forest":90,"aqua":89,"teal":90,"lightblue":94,"blue":93,"indigo":94,"purple":95,"royalpurple":95,"magenta":95,"pink":95,"dustypink":95,"red":94},"light":{"orange":83,"gold":77,"yellow":77,"chartreuse":80,"green":85,"forest":80,"aqua":78,"teal":82,"lightblue":86,"blue":87,"indigo":89,"purple":92,"royalpurple":92,"magenta":91,"pink":90,"dustypink":90,"red":89},"abitlight":{"orange":72,"gold":65,"yellow":64,"chartreuse":67,"green":73,"forest":67,"aqua":64,"teal":69,"lightblue":74,"blue":78,"indigo":82,"purple":85,"royalpurple":85,"magenta":83,"pink":82,"dustypink":82,"red":82},"medium":{"orange":61,"gold":56,"yellow":53,"chartreuse":55,"green":58,"forest":52,"aqua":50,"teal":53,"lightblue":60,"blue":68,"indigo":74,"purple":76,"royalpurple":75,"magenta":73,"pink":70,"dustypink":71,"red":71},"abitdark":{"orange":53,"gold":50,"yellow":47,"chartreuse":44,"green":45,"forest":39,"aqua":40,"teal":43,"lightblue":48,"blue":60,"indigo":67,"purple":66,"royalpurple":65,"magenta":61,"pink":60,"dustypink":60,"red":60},"dark":{"orange":48,"gold":44,"yellow":40,"chartreuse":35,"green":36,"forest":30,"aqua":32,"teal":36,"lightblue":39,"blue":53,"indigo":59,"purple":58,"royalpurple":56,"magenta":49,"pink":51,"dustypink":50,"red":51},"evendarker":{"orange":40,"gold":37,"yellow":33,"chartreuse":27,"green":29,"forest":24,"aqua":26,"teal":31,"lightblue":32,"blue":48,"indigo":51,"purple":50,"royalpurple":47,"magenta":40,"pink":42,"dustypink":41,"red":42},"ultradark":{"orange":34,"gold":31,"yellow":29,"chartreuse":23,"green":24,"forest":20,"aqua":22,"teal":27,"lightblue":27,"blue":40,"indigo":41,"purple":42,"royalpurple":39,"magenta":33,"pink":35,"dustypink":35,"red":35},"almostblack":{"orange":28,"gold":26,"yellow":26,"chartreuse":20,"green":20,"forest":16,"aqua":19,"teal":24,"lightblue":24,"blue":33,"indigo":34,"purple":35,"royalpurple":32,"magenta":28,"pink":30,"dustypink":30,"red":31}}
廣黃

我相信你只需要做一個countourf

plt.contourf(df, cmap='RdYlBu')
plt.xticks(range(df.shape[1]), df.columns, rotation=90)
plt.yticks(range(df.shape[0]), df.index)
plt.show()

輸出:

在此處輸入圖片說明

或熱圖:

import seaborn as sns
sns.heatmap(df, cmap='RdYlBu')

輸出:

在此處輸入圖片說明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章