Hello everyone! I don’t know how to explain this problem
I created a gauge chart by using matplotlib; but I don’t want to see this drop down text above the chart. How do I disable it?
Hello everyone! I don’t know how to explain this problem
I created a gauge chart by using matplotlib; but I don’t want to see this drop down text above the chart. How do I disable it?
hi @baligoyem, Welcome to the community!
Could you please share your code. This makes helping you easier.
Thanks!
hi @maarten-betman, thanks for replying! Here is my code for creating a gauge chart:
import matplotlib.pyplot as plt
import streamlit as st
import pandas as pd
import numpy as np
from matplotlib import cm
from matplotlib.patches import Circle, Wedge, Rectangle
def degree_range(n):
start = np.linspace(0, 180, n + 1, endpoint=True)[0:-1]
end = np.linspace(0, 180, n + 1, endpoint=True)[1::]
mid_points = start + ((end - start) / 2.)
return np.c_[start, end], mid_points
def rot_text(ang):
rotation = np.degrees(np.radians(ang) * np.pi / np.pi - np.radians(90))
return rotation
def gauge(labels=['LOW', 'MEDIUM', 'HIGH', 'VERY HIGH', 'EXTREME'], \
colors='jet_r', arrow=1, title='', fname=False):
N = len(labels)
if arrow > N:
raise Exception("\n\nThe category ({}) is greated than \
the length\nof the labels ({})".format(arrow, N))
if isinstance(colors, str):
cmap = cm.get_cmap(colors, N)
cmap = cmap(np.arange(N))
colors = cmap[::-1, :].tolist()
if isinstance(colors, list):
if len(colors) == N:
colors = colors[::-1]
else:
raise Exception("\n\nnumber of colors {} not equal \
to number of categories{}\n".format(len(colors), N))
fig, ax = plt.subplots()
ang_range, mid_points = degree_range(N)
labels = labels[::-1]
patches = []
for ang, c in zip(ang_range, colors):
# sectors
patches.append(Wedge((0.,0.), .4, *ang, facecolor='w', lw=2))
# arcs
patches.append(Wedge((0., 0.), .4, *ang, width=0.10, facecolor=c, lw=2, alpha=0.5))
[ax.add_patch(p) for p in patches]
for mid, lab in zip(mid_points, labels):
ax.text(0.35 * np.cos(np.radians(mid)), 0.35 * np.sin(np.radians(mid)), lab, \
horizontalalignment='center', verticalalignment='center', fontsize=14, \
fontweight='bold', rotation=rot_text(mid))
r = Rectangle((-0.4, -0.1), 0.8, 0.1, facecolor='w', lw=2)
ax.add_patch(r)
ax.text(0, -0.05, title, horizontalalignment='center', \
verticalalignment='center', fontsize=22, fontweight='bold')
pos = mid_points[abs(arrow - N)]
ax.arrow(0, 0, 0.225 * np.cos(np.radians(pos)), 0.225 * np.sin(np.radians(pos)), \
width=0.04, head_width=0.09, head_length=0.1, fc='k', ec='k')
ax.add_patch(Circle((0, 0), radius=0.02, facecolor='k'))
ax.add_patch(Circle((0, 0), radius=0.01, facecolor='w', zorder=11))
plt.title("DQ Score", fontsize = 20)
ax.set_frame_on(False)
ax.axes.set_xticks([])
ax.axes.set_yticks([])
ax.axis('equal')
plt.tight_layout()
if fname:
fig.savefig(fname, dpi=200)
gauge(labels=['VERY LOW', 'LOW', 'MEDIUM', 'HIGH'], \
colors=["#1b0203", "#ED1C24", '#FFCC00', '#007A00'], arrow=2, title="something here")
st.pyplot()
Hi @baligoyem,
I think it’s due to this list comprehension: [ax.add_patch(p) for p in patches]
.
Since you’re adding patches to ax
you don’t have to store it under a variable, but when adding a patch matplotlib always return the patch object. That’s why streamlit is showing a list of patch objects.
Fix would be to store it under a variable, foo = [ax.add_patch(p) for p in patches]
.
Hi @maarten-betman,
It’s fixed, thanks a lot!
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.