import matplotlib.pyplot as plt
def display_diagram(value, percentage):
fig, ax = plt.subplots()
# Plotting a bar chart with a single value
ax.bar(0, value, color='blue')
ax.set_xlim(-1, 1)
ax.set_ylim(0, value * 1.25)
# Adding the percentage label above the bar
ax.text(0, value + value*0.1, f'{percentage}%', ha='center')
# Removing axis labels and ticks
ax.set_xticks([])
ax.set_yticks([])
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
# Displaying the diagram
plt.show()
# Example usage
display_diagram(10, 50) # Displaying a bar chart with value 10 and percentage 50%
In this example, we define a display_diagram function that takes the value and percentage as input parameters.
Inside the function, we create a figure and axes object using plt.subplots(). Then, we plot a bar chart with a single value using ax.bar(0, value, color='blue').
We set the x-axis limits to -1 and 1 using ax.set_xlim(-1, 1) to ensure the bar is centered. We also set the y-axis limits to 0 and value * 1.25 using ax.set_ylim(0, value * 1.25) to leave some extra space above the bar.
Next, we add the percentage label above the bar using ax.text(0, value + value*0.1, f'{percentage}%', ha='center'). Here, we use the ha='center' parameter to center-align the label horizontally.
Finally, we remove the axis labels, ticks, and spines using various ax.set_xticks([]), ax.set_yticks([]), and ax.spines['...'].set_visible(False) commands to create a clean and minimalistic diagram. The plt.show() function is used to display the diagram.