gpt4 book ai didi

matplotlib - 在 Matplotlib 中更改箱形图/ fiddle 图的轴标签

转载 作者:行者123 更新时间:2023-12-01 12:46:49 25 4
gpt4 key购买 nike

稍微修改这里的代码:http://pyinsci.blogspot.com/2009/09/violin-plot-with-matplotlib.html如下,我可以得到一个用 Python 生成的 fiddle 图,如下所示:

# Import modules
import pylab as pl
from scipy import stats
import numpy as np

# Function for Violin Plot

def violin_plot(ax,data,groups,bp=False):
'''Create violin plot along an axis'''
dist = max(groups) - min(groups)
w = min(0.15*max(dist,1.0),0.5)
for d,p in zip(data,groups):
k = stats.gaussian_kde(d) #calculates the kernel density
m = k.dataset.min() #lower bound of violin
M = k.dataset.max() #upper bound of violin
x = np.arange(m,M,(M-m)/100.) # support for violin
v = k.evaluate(x) #violin profile (density curve)
v = v/v.max()*w #scaling the violin to the available space
ax.fill_betweenx(x,p,v+p,facecolor='y',alpha=0.3)
ax.fill_betweenx(x,p,-v+p,facecolor='y',alpha=0.3)
if bp:
ax.boxplot(data,notch=1,positions=pos,vert=1)

groups = range(3)
a = np.random.normal(size=100)
b = np.random.normal(size=100)
c = np.random.normal(size=100)

data = np.vstack((a,b,c))
fig = pl.figure()
ax = fig.add_subplot(111)
violin_plot(ax,data,groups,bp=0)
pl.show()

这会生成如下图:

enter image description here

我想做的是改变刻度线的标签,这样不是 -0.5 到 2.5 0.5 数字,只有一个“A”,其中 0.0 是,一个“B”在 1.0 和一个“C"在 2.0。

有没有简单的方法来做到这一点?

最佳答案

在调用 pl.show 之前,使用:

ax.set_xticks([0, 1, 2])
ax.set_xticklabels(['A', 'B', 'C'])

关于matplotlib - 在 Matplotlib 中更改箱形图/ fiddle 图的轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15079081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com