Reputation: 7689
I am plotting a bar chart using seaborn and matplotlib. I would like to annotate the plot with both words and a line.
Here is my strategy for generating the figure (apologies for providing plot_data
but it is rather large):
plt.figure()
ax = seaborn.barplot(x='cell_line', y='DeltaCt', data=plot_data, hue='time')
plt.title('Baseline: {}'.format(g))
plt.ylabel('DeltaCt')
plt.xlabel('')
trans = ax.get_xaxis_transform()
ax.annotate('Neonatal', xy=(0.4, -0.1), xycoords=trans)
plt.show()
However I need another black line on this plot between the x-axis and the 'Neonatal' annotation. Like this:
Upvotes: 1
Views: 5497
Reputation: 339705
Some related questions:
Here you want a vertical line, but that line would need to be in data coordinates along the x axis. Hence you can use the ax.get_xaxis_transform()
. To make the line visible outside the axes use clip_on = False
.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plot_data = pd.DataFrame({"cell_line": np.random.choice(list("ABCDEFG"), size=150),
'DeltaCt' : np.random.rayleigh(5,size=150),
"time":np.random.choice([0,96], size=150)})
plt.figure()
ax = sns.barplot(x='cell_line', y='DeltaCt', data=plot_data, hue='time',
order=list("ABCDEFG"))
plt.title('Baseline: {}'.format("H"))
plt.ylabel('DeltaCt')
plt.xlabel('')
trans = ax.get_xaxis_transform()
ax.annotate('Neonatal', xy=(1, -.1), xycoords=trans, ha="center", va="top")
ax.plot([-.4,2.4],[-.08,-.08], color="k", transform=trans, clip_on=False)
plt.show()
Upvotes: 7