Logarithm in Seaborn

Viewed 86

I am using seaborn to do a box plot, and for that I am using the following code

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()

But what I want to do is that for Y-axis I want to take the logarithm of the Y values. How to do this here?

Is this the right way?

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)
1 Answers

I think you can create another column called val_log by:

df['val_log'] = np.log(df['val'].values)

and create the plot:

ax = sns.boxplot(data=df, x="num1_5_10", y="val_log", hue="cat", zorder=0.9, dodge=False)
Related