NYC High School Analysis

Hi all!I spent 3 days on this guided project. I would love to have some feedback on my graph and analysis part.

I try to label the value on the bar chart, but somehow I can’t do it with the negative number and cant change the label position. If you have a way to do it, please help.

[NYC High School Analysis.ipynb|attachment](upload://5XxPh3fA17psSCFNecK8SDSyrMV.ipynb) (333.4 KB)
1 Like

For some reason your download link isn’t being formatted properly and the Dataquest bot hasn’t added a link to view it in a separate tab.

This link will bring up the notebook viewer.

Click here to view the jupyter notebook file in a new tab


Hi @candiceliu93,

I just checked your post and it seems to be caused due to a formatting issue.

<!-- 3. upload your notebook (.ipynb file) below-->[NYC High School Analysis.ipynb|attachment](upload://5XxPh3fA17psSCFNecK8SDSyrMV.ipynb) (333.4 KB)

To prevent this from happening in the future, make sure that the file attachment code is in a new line like this:

<!-- 3. upload your notebook (.ipynb file) below-->
[NYC High School Analysis.ipynb|attachment](upload://5XxPh3fA17psSCFNecK8SDSyrMV.ipynb) (333.4 KB)

If it still doesn’t generate the nbviewer link, you can manually add using the method explained in this post:

Hope this helps :slightly_smiling_face:


1 Like

Finally I figured out the way to show labels in negative axis, hope so this helps.

# Plot the figure.
plt.figure(figsize=(12, 8))
ax = correlations[survey_fields].plot(kind='bar')
ax.set_xlabel('Survey Feilds')
x_labels = survey_fields

def add_value_labels(ax, spacing=5):
    """Add labels to the end of each bar in a bar chart.

        ax (matplotlib.axes.Axes): The matplotlib object containing the axes
            of the plot to annotate.
        spacing (int): The distance between the labels and the bars.

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

        # Number of points between bar and label. Change to your liking.
        space = spacing
        # Vertical alignment for positive values
        va = 'bottom'

        # If value of bar is negative: Place label below bar
#         print(y_sign)
        if y_sign < 0:
            # Invert space to place label below
            space *= -1
            y_value *= -1  #To correct the label position
#             print(space)
            # Vertically align label at top
            va = 'top'

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

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

# Call the function above. All the magic happens there.

In above answer also note that Stackoverflow Solution help , the problem with stackoverflow was that y_value = rect.get_height() was always positive, so
if y_value < 0: was failing , i modified by determining whether y is positive or negative and adjust y_value accordingly.