Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

add values to add value in a matplotlib image

# Bring some raw data.
frequencies = [6, -16, 75, 160, 244, 260, 145, 73, 16, 4, 1]

freq_series = pd.Series(frequencies)

y_labels = [108300.0, 110540.0, 112780.0, 115020.0, 117260.0, 119500.0, 
            121740.0, 123980.0, 126220.0, 128460.0, 130700.0]

# Plot the figure.
plt.figure(figsize=(12, 8))
ax = freq_series.plot(kind='barh')
ax.set_title('Amount Frequency')
ax.set_xlabel('Frequency')
ax.set_ylabel('Amount ($)')
ax.set_yticklabels(y_labels)
ax.set_xlim(-40, 300) # expand xlim to make labels easier to read

rects = ax.patches

# For each bar: Place a label
for rect in rects:
    # Get X and Y placement of label from rect.
    x_value = rect.get_width()
    y_value = rect.get_y() + rect.get_height() / 2

    # Number of points between bar and label. Change to your liking.
    space = 5
    # Vertical alignment for positive values
    ha = 'left'

    # If value of bar is negative: Place label left of bar
    if x_value < 0:
        # Invert space to place label to the left
        space *= -1
        # Horizontally align label at right
        ha = 'right'

    # Use X value as label and format number with one decimal place
    label = "{:.1f}".format(x_value)

    # Create annotation
    plt.annotate(
        label,                      # Use `label` as label
        (x_value, y_value),         # Place label at end of the bar
        xytext=(space, 0),          # Horizontally shift label by `space`
        textcoords="offset points", # Interpret `xytext` as offset in points
        va='center',                # Vertically center label
        ha=ha)                      # Horizontally align label differently for
                                    # positive and negative values.

plt.savefig("image.png")
Comment

PREVIOUS NEXT
Code Example
Python :: infinty in python 
Python :: opencv cartoonizer script 
Python :: numpy get length of list 
Python :: django model meta ordering multiple ordering 
Python :: lambda if else nothing python 
Python :: numpy documentation realpython 
Python :: python lxml get parent 
Python :: k means image classification 
Python :: two legend left and right x asix matplotlib 
Python :: how to def a variable in python 
Python :: creer des disques en python tkinter 
Python :: python run scp command 
Python :: how to save an object in python to disk 
Python :: ModelCheckpoint 
Python :: zeromq pub sub example python 
Python :: Applies a function to all elements of this RDD. 
Python :: python slice second element of list of lists 
Python :: PyQT5 reset color 
Python :: how to upgrade python from 2.7 to 2.9 on ubuntu 14.04 
Python :: python sort isdigit 
Python :: for loop does not work with open 
Python :: pandas remove rows based on DATETIME column year 
Python :: get derivative of interp1d 
Python :: tf get devices 
Python :: what does 0 for in array mean python 
Python :: ipython widget display 
Python :: ensure string length 2 python 
Python :: convert outlook email to text file python 
Python :: when was python 3.7 released 
Python :: JET token authentication in Django UTC-1 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =