Issue
I am trying to add a few labels below the x-axis using matplotlib. I have 5-6 labels which I want to plot below the xaxis. The text of these labels will vary hence, having a single setting to render the labels is not possible. I have a minimal example given below.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# Generate sample data
frame_width = 1920
frame_height =1080
##plotting function begins
fig, ax = plt.subplots(1,1,figsize=(frame_width/100,frame_height/100))
ax = plt.gca()
fig = plt.gcf()
fig_width = fig.get_figwidth() * fig.dpi
txtcolor='White'
text_font_size=12
textfont='Arial'
# Show only the x-axis
ax.set_xlim(1, 20)
txt1="ATTENTION"
a1 = plt.figtext(0.15, 0.070, txt1, ha='left',va='center',weight='bold',c=txtcolor,family=textfont ,fontsize=text_font_size, bbox=dict(facecolor='g', edgecolor='g'))
txt1_width = a1.get_window_extent().width / fig_width
txt2_x = txt1_width + 0.15 + 0.02
txt2="CONVICTION"
a2=plt.figtext(txt2_x, 0.070, txt2, ha='left',va='center',weight='bold',c=txtcolor,family=textfont ,fontsize=text_font_size, bbox=dict(facecolor='c', edgecolor='c'))
txt2_width = a2.get_window_extent().width / fig_width
txt3_x = txt2_width + 0.15 + 0.03 + txt1_width
txt3="INTEREST IN SPORTS"
a3=plt.figtext(txt3_x, 0.070, txt3, ha='left',va='center',weight='bold',c=txtcolor,family=textfont ,fontsize=text_font_size, bbox=dict(facecolor='m', edgecolor='m'))
txt3_width = a3.get_window_extent().width / fig_width
txt4_x = txt2_width + 0.15 + 0.03 + txt1_width + txt3_width
plt.show()
I am trying to take the width of the text + add a padding so that the next text does not overlap. However, as the text varies in length, I have to change the setting to avoid overlapping or creating a larger gap between the text labels. How can I dynamically render it so that the text labels look uniformly spaced irrespective of the size of the text.
Solution
If you use annotate
, you can place text relative to any other artist, including another annotation. Here I place the first text relative to the axes. Subsequent annotations are placed by putting the text 1.5 font sizes (xytext=(1.5, 0)
) to the right of the righthand side of the previous annotation (xy=(1, 0)
).
import matplotlib.pyplot as plt
# Generate sample data
frame_width = 1920
frame_height =1080
##plotting function begins
fig, ax = plt.subplots(1,1,figsize=(frame_width/100,frame_height/100))
ax = plt.gca()
fig = plt.gcf()
fig_width = fig.get_figwidth() * fig.dpi
txtcolor='White'
text_font_size=12
textfont='Arial'
# Show only the x-axis
ax.set_xlim(1, 20)
txt1="ATTENTION"
ann = ax.annotate(txt1, (0, -0.1), ha='left',va='center',weight='bold',
c=txtcolor, family=textfont, fontsize=text_font_size,
xycoords='axes fraction',
bbox=dict(facecolor='g', edgecolor='g'))
for txt, color in zip(["CONVICTION", "INTEREST IN SPORTS"], ['c', 'm']):
ann = ax.annotate(txt, (1, 0.5), xycoords=ann, xytext=(1.5, 0),
textcoords='offset fontsize', va='center', weight='bold',
c=txtcolor, family=textfont, fontsize=text_font_size,
bbox=dict(facecolor=color, edgecolor=color))
plt.show()
Answered By - RuthC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.