python - Seaborn 中的对数
问题描述
我正在使用 seaborn 做箱形图,为此我使用以下代码
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
df = pd.read_csv("Data_P64.txt", header=None, names=["val"])
df["cat"] = np.tile(np.repeat(["A", "B", "C"], 5), 7)
df["num1_5_10"] = np.repeat([1, 5, 10, 20, 30, 40, 50], 15)
ax = sns.boxplot(data=df, x="num1_5_10", y="val", hue="cat", zorder=0.9, dodge=False)
#add the lines connecting the median values
sns.pointplot(data=df, x="num1_5_10", y="val", hue="cat", estimator=np.median, ci=None, markers="None", ax=ax)
#remove duplicate entries, setting the legend outside the graph, new title
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[:3], labels[:3], bbox_to_anchor=(1, 1), title="C++ for the win")
plt.tight_layout()
plt.show()
但我想要做的是,对于 Y 轴,我想取logarithm
Y 值中的一个。如何在这里做到这一点?
这是正确的方法吗?
df = pd.read_csv("Data_P64.txt", header=None, names=["val"])
df["cat"] = np.tile(np.repeat(["A", "B", "C"], 5), 7)
df["num1_5_10"] = np.repeat([1, 5, 10, 20, 30, 40, 50], 15)
df['val_log'] = np.log2(df['val'].values)
ax = sns.boxplot(data=df, x="num1_5_10", y="val_log", hue="cat", zorder=0.9, dodge=False)
解决方案
我认为您可以通过以下方式创建另一个名为 val_log 的列:
df['val_log'] = np.log(df['val'].values)
并创建情节:
ax = sns.boxplot(data=df, x="num1_5_10", y="val_log", hue="cat", zorder=0.9, dodge=False)
推荐阅读
- parsing - 使用 ELK 中的摄取节点处理器以 yyyy.MM.dd HH.mm.ss.SSSSSS 格式解析日期
- python - 从相似度矩阵创建 NetworkX 图
- centos - 错误'bad pad value "2m": must be positive screen distance' when using tk_messageBox on a wayland display (CentOS)
- arm - 使用 ARM M0 和 GDB 进行监控模式调试
- elasticsearch - 当我们在弹性搜索中使用倒排索引并获取包含特定单词/单词的所有文档时,在搜索中使用正向索引有什么用?
- python - 如何在 SciPy 正交距离回归 (ODR) 中使用 Lambda 函数?
- c# - C# ASP.NET Core Web API 包含在哪里
- javascript - 根据之前的下拉选择选择另一个下拉值
- python - ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()。需要帮助解决错误
- reactjs - 使用带有私有路由的上下文 API 响应登录