[FIXED] Hinzufügen von Punkten zur Diagrammleiste mit Matplot

Ausgabe

Ich erstelle einige Balkendiagramme für alle Spalten in meinem df, aber ich möchte einige Punkte auf der Leiste hinzufügen. Das Originalbild, das ich mit meinem Code erzeuge (am Ende des Beitrags), sind diese:

Geben Sie hier die Bildbeschreibung ein

Und das will ich:

Geben Sie hier die Bildbeschreibung ein

Ist das mit Python möglich? Vorschläge oder Hilfe mit dem Code bitte

for i in df.columns:
    #print(i)
    ii= df[i].value_counts()
    #dfclean[i].value_counts().plot(kind="bar", figsize=(15,7), color="#61d199", title=i)
    fig, ax = plt.subplots(figsize=(15,7))
    myList = ii.items()
    myList = sorted(myList) # or myList for not sorted by results
    x, y = zip(*myList) 

    for index in range(len(x)):
        ax.text(x[index], y[index], y[index], size=13)

    #plt.bar(x, y, color=('black', 'red', 'green', 'blue', 'cyan'))
    #plt.bar(x, y, color=('#22314A'))
    plt.bar(x, y, color=('#22314A', '#E35855', '#1293C4', '#FFFFFF', '#CFAB2B', '#008080', '#AE0E36'))
    
    #plt.bar(x, y, color=('#22314A', '#E35855', '#1293C4', '#FFFFFF', '#CFAB2B'), width=0.1)# Modify width
    plt.xlabel("Answers", fontsize=20)
    plt.ylabel('No. answers', fontsize=20)
    plt.title(i, fontsize=20)
    plt.xticks(rotation=45, fontsize=16)#change rotation
    plt.yticks(fontsize=16)#change rotation
    
    i = i.replace(" ", "_")
    i = i.replace("[", "_")
    i = i.replace("]", "_")
    i = i.replace("?", "_")
    i = i.replace("/", "_")
    i = i.replace(".", "_")
    i = i.replace(":", "_")
    #print(i)
    plt.style.use('seaborn')
    plt.savefig(f'{i}.png', bbox_inches='tight')

Lösung

Um einem Diagramm eine Ellipse hinzuzufügen, verwenden Sie Artist. Weitere Informationen finden Sie in der Referenz . Basierend auf dem Beispieldiagramm in der Referenz werden die Füllfarbe und die Kantenfarbe vorbereitet und Anmerkungen und Ellipsen in einem Schleifenprozess hinzugefügt.

import matplotlib.pyplot as plt
import matplotlib.patches as patches

fig, ax = plt.subplots()

fruits = ['apple', 'blueberry', 'cherry', 'orange']
counts = [40, 100, 30, 55]
bar_labels = ['black', 'red', 'blue', 'sand']
bar_colors = ['#22314A', '#E35855', '#1293C4', '#CFAB2B']
edge_color = ['#22314A', '#E35855', '#1293C4', 'black']

rects1 = ax.bar(fruits, counts, label=bar_labels, color=bar_colors)

ax.set_ylabel('fruit supply')
ax.set_title('Fruit supply by kind and color')
ax.legend(title='Fruit color')
ax.bar_label(rects1, padding=2)
for s in ['top','bottom','right','left']:
    ax.spines[s].set_visible(False)
    
ax.set_xlabel("Answers", fontsize=20)
ax.set_ylabel('No. answers', fontsize=20)
ax.set_title('Action', fontsize=20)
ax.set_ylim([0,130])
plt.xticks(rotation=45, fontsize=16)
plt.yticks(fontsize=16)

offset = 15
for i,(cnt,c,e) in enumerate(zip(counts,bar_colors, edge_color)): 
    e = patches.Ellipse(xy=(i, cnt+offset), width=0.4, height=15, fc=c, ec=e)
    ax.text(i, cnt+offset, str(cnt), color='white', ha='center', va='center')
    ax.add_patch(e)

plt.style.use('seaborn-v0_8')
plt.show()

Geben Sie hier die Bildbeschreibung ein


Beantwortet von –
r-Anfänger


Antwort geprüft von –
Gilberto Lyons (FixError Admin)

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like

[FIXED] Pipdeptree-JSON-Ergebnis in CSV

Ausgabe Das json-Ergebnis von pipdeptreehat die Form: [ { "package": { "key": "ansible-base", "package_name": "ansible-base", "installed_version": "2.10.8", "required_version":…