我一直在嘗試繪制一個漂亮的小提琴圖,以使用 Python 中的 Matplotlib 庫來呈現我的資料。這是我使用的代碼:
ax.violinplot(vdistances,showmeans=False,showmedians=True)
ax.set_title('Distance analysis for molecule 1')
ax.set_xlabel('Atomic distances')
ax.set_ylabel('Distances in Amstrongs')
ax.set_xticks([x for x in range(1,len(distances) 1)])
plt.show()
這就是我想出的:

我一直想知道的是,是否可以根據分布的廣泛程度為每把小提琴分配相同顏色的不同音調,因此資料越分散。
uj5u.com熱心網友回復:
您可以遍歷生成的小提琴,提取它們的高度并使用它來設定顏色:
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
from matplotlib.cm import ScalarMappable
import numpy as np
fig, ax = plt.subplots(figsize=(12, 5))
vdistances = np.random.normal(np.random.uniform(10, 20, (20, 10)), np.random.uniform(2, 5, (20, 10)))
violins = ax.violinplot(vdistances, showmeans=False, showmedians=True)
ax.xaxis.set_major_locator(MultipleLocator(1))
heights = [violin.get_paths()[0].get_extents().height for violin in violins['bodies']]
norm = plt.Normalize(min(heights), max(heights))
cmap = plt.get_cmap('plasma')
for violin, height in zip(violins['bodies'], heights):
violin.set_color(cmap(norm(height)))
violin.set_alpha(0.5)
plt.colorbar(ScalarMappable(norm=norm, cmap=cmap), alpha=violin.get_alpha(), label='Violin Extent', ax=ax)
plt.tight_layout()
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/461109.html
標籤:Python python-3.x matplotlib 图形 小提琴情节
上一篇:有人可以幫我為這個python代碼寫一個影片函式嗎?
下一篇:更新matplotlib上的繪圖
