简体   繁体   English

Python Matplotlib:图文本将不会向左对齐

[英]Python Matplotlib: plot text will not align left

I am trying to get the values for red dots (for values of 'y2') to plot to the left of the dots. 我正在尝试获取红色点的值(“ y2”的值)以绘制到点的左侧。 I'm using ha='left' but to no avail. 我正在使用ha ='left',但无济于事。 I tried the following two aproaches: 我尝试了以下两个方法:

for a,b in zip(ind, y2):
    plt.text(a, b, str(b), fontsize=14, color='black', va='center', ha='left)

#OR:
for i, text in enumerate(y2):
    ax.annotate(str(text), (ind[i],y2[i]), ha='left')

Here is the code in its entirety: 这是完整的代码:

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
df0=pd.read_csv('sample1.csv')

import matplotlib.pyplot as plt

# You typically want your plot to be ~1.33x wider than tall.
# Common sizes: (10, 7.5) and (12, 9)
fig, ax = plt.subplots(1, 1, figsize=(12, 9))
plt.xticks(ind, x_axis, fontsize=14)
#Set y-axis font size
plt.yticks(fontsize=12)
x_axis = ['a','b','c']
y_axis = [2,8,10]

y2 = [3, 5, 9.5]

# Remove the plot frame lines. They are unnecessary here.
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)

#0 = 'a', 1 = 'b, 2 = 'c'
ind = np.arange(len(x_axis))


#remove axis ticks save for left (y-axis):
plt.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom='off',      # ticks along the bottom edge are off
    top='off')         # ticks along the top edge are off


plt.tick_params(
    axis='y',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    right='off')

plt.title('Super Awesome Title Goes Here\n'
          'and Is Continued Here', fontsize=18, va='center')
#Plot dots, cyan circles, no edges, transparency = 0.7 (1 is opaque))
# larger circles
plt.plot(ind, y_axis, 'co', markeredgecolor='none', alpha=1, markersize=40)
#plt.axis(0,10,0,3)

plt.plot(ind, y2, 'ro', markeredgecolor='none', alpha=.7, markersize=40)

#a and b are "where" to plot the label, str(b) is what to plot for the label
#add spaces to put label to the right
for a,b in zip(ind, y_axis):
    plt.text(a, b, "     "+str(b), fontsize=14, color='black', va='center')

for a,b in zip(ind, y2):
    plt.text(a, b, str(b), fontsize=14, color='black', va='center', ha='left')

    #Cell padding between categories
plt.xlim([min(ind) - 0.25, max(ind) + 0.25])
plt.ylim([0, max(y_axis) + 2])
plt.show()

The ha and va arguments specify which edges of the text bounding box will be aligned to the specified x,y coordinates. hava参数指定文本边界框的哪些边缘将与指定的x,y坐标对齐。 If you want your text to appear to the left of your dot, you need to align it according to the right hand edge of the bounding box: 如果要使文本显示在圆点的左侧 ,则需要根据边界框的边缘将其对齐:

from matplotlib import pyplot as  plt

fig, ax = plt.subplots(1, 1)
ax.hold(True)
ax.plot(0, 0, '+', ms=60)
ax.text(0, 0, 'align by top right corner', ha='right', va='top', fontsize=22)
ax.text(0, 0, 'align by bottom left corner', ha='left', va='bottom', fontsize=22)
ax.set_axis_off()
plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM