python-3.x - 设置手动 x 轴刻度小提琴图
问题描述
我正在尝试使用 matplotlib 构建小提琴图。
根据此处提供的示例设置手动 X 轴刻度时,我没有这样做。我错过了什么?
这是一个 MWE
#!/usr/bin/env python3
import os
import numpy as np
import warnings
import matplotlib.pyplot as plt
import matplotlib.cbook
import matplotlib as mpl
warnings.filterwarnings("ignore",category=matplotlib.cbook.mplDeprecation)
OUTPUT_PATH=os.getcwd() + "/"
# Dots per inch for figure.
DPI = 500
def test_plot():
fig = plt.figure()
vector_size=100
bucket2 = np.random.rand(vector_size)
bucket3 = np.random.rand(vector_size)
bucket4 = np.random.rand(vector_size)
bucket5 = np.random.rand(vector_size)
bucket6 = np.random.rand(vector_size)
pos = [1,2,3,4,5]
data= [np.array(bucket2), np.array(bucket3), np.array(bucket4), np.array(bucket5), np.array(bucket6)]
axes1 = fig.add_subplot(111)
axes1.violinplot(data, pos, points=100, widths=0.7, showmeans=False, showextrema=True, showmedians=True)
axes1.set_xlabel('x-axis')
axes1.set_ylabel('y-axis')
xticks_t = ["",".1-.2", ".2-.3", ".3-.4", ".4-.5", ">.5"]
axes1.set_xticklabels(xticks_t)
axes1.set_xlim([0, 5])
axes1.spines['right'].set_visible(False)
axes1.spines['top'].set_visible(False)
axes1.xaxis.set_ticks_position('bottom')
axes1.yaxis.set_ticks_position('left')
fig.tight_layout()
file_name = 'test_violin.pdf'
fig.savefig(OUTPUT_PATH + str(file_name), bbox_inches='tight', dpi=DPI, pad_inches=0.1)
fig.clf()
plt.close()
pass
test_plot()
解决方案
推荐阅读
- r - 使用 R 中的 table() 对数据帧中具有不同长度的变量进行计数
- android - Android - 使用键盘应用程序保护用户的隐私
- python - 使用python中的范围从结尾打印字符串的字符到开头
- linux - 从汇编程序调用时 dlsym 崩溃
- javascript - 如何使用 twilio 通过 whatsapp 发送本地存储的媒体文件 .. node js express
- java - 将 LocalTime 分钟/秒格式化为整数 Java
- html - 如何从我的 Django 网站下载文件?
- c# - 如何在 Xamarin Forms Android 应用程序的 BackgroundService 中集成 rabbitmq 客户端?
- python - 为什么这个 PyGTK 菜单是空的?
- r - 使用 R 中的 clusGAP 函数查找集群的数量