So, the script below requests data from postgres database and draws a diagram. The requested data is a table with 4 columns (ID, Object, Percentage, Color)
.
The data:
result = [
(1, 'Apple', 10, 'Red'),
(2, 'Blueberry', 40, 'Blue'),
(3, 'Cherry', 94, 'Red'),
(4, 'Orange', 68, 'Orange')
]
import pandas as pd
from matplotlib import pyplot as plt
import psycopg2
conn = psycopg2.connect(
host="localhost",
port="5432",
database="db",
user="user",
password="123")
cur = conn.cursor()
cur.callproc("test_stored_procedure")
result = cur.fetchall()
cur.close()
conn.close()
print(result)
result = pd.DataFrame(result, columns=['ID', 'Object', 'Percentage', 'Color'])
fruits = result.Object
counts = result.Percentage
labels = result.Color
s = 'tab:'
bar_colors = [s + x for x in result.Color]
fig, ax = plt.subplots()
for x, y, c, lb in zip(fruits, counts, bar_colors, labels):
ax.bar(x, y, color=c, label=lb)
ax.set_ylabel('fruit supply')
ax.set_title('Fruit supply by kind and color')
ax.legend(title='Fruit color', loc='upper left')
plt.show()
Result:
As you can see in the legend "Red"
label is shown twice.
I tried several different examples of how to fix this, but unfortunately no one worked out. F.e.:
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles, labels)
source https://stackoverflow.com/questions/76392467/how-to-consolidate-labels-in-legend
Comments
Post a Comment