I have a scatter plot which represents a KPI. Below the KPI value I have a green rectangle, above a red rectangle created both with "fig.add_hrect()". I add text to indicate "KPI not satisfied" using:
fig.add_hrect(y0=sbf._KPI_CODERES_THRESHOLD, y1=residual_max,
annotation_text='KPI not satisfied', annotation_position='top',
fillcolor='red', opacity=0.05, line_width=0)
The text is in black and I cannot find a way to have the annotation text colored red.
Any help appreciated,
Tx/ALain
You can use fig.update_annotations() to change the annotation text. Below is a sample of the same...
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="petal_length", y="petal_width") ## My dummy plot
fig.add_hrect(y0=1, y1=1, annotation_text='KPI not satisfied', annotation_position='top',
fillcolor='red', opacity=0.05, line_width=0)
fig.update_annotations(font=dict(family="sans serif", size=18, color="red"))
Related
I'm trying to make a network graph with NetworkX and am stuck with two problems. I can't get the background color to be transparent or white, and I'm stuck with a light grey background. When I try to save this graph as a png it gives me an empty image.
Also, I'm trying to add a legend to the graph based on the colors of the nodes, but I am not sure what I'm doing wrong there.
Can someone help with a fix for this?
plt.figure(figsize=(24,18))
plt.rcParams['figure.facecolor'] = 'white' # DOESN'T WORK
G = nx.Graph()
pos = nx.random_layout(G)
nx.draw_networkx_nodes(G, pos, alpha=0.5, linewidths=3, node_color=[x[1]['color'] for x in G.nodes(data=True)], node_size=[150*x[1]['size'] for x in G.nodes(data=True)])
nx.draw_networkx_labels(G, pos, font_size=10, font_color='#1b1b1b', font_weight='bold', font_family='sans-serif')
nx.draw_networkx_edges(G, pos, edge_color='grey', width=[0.4*x[2]['width'] for x in G.edges(data=True)])
plt.legend(dict_colors.values(), dict_colors.keys()) # DOESN'T WORK
plt.savefig("Network.png", transparent=True) # DOESN'T WORK
plt.show()
Fixed the legend with -
import Color from colour
plt.legend(handles=[Patch(facecolor=Color(val).hex, label=key) for key, val in dict_colors.items()])
Is it possible to specify the color of center of colorbar in seaborn heatmap?
As example the ceneter of colorbar of the following heat map is 70, and I want to be specified with black color instead of white.
Thanks in advance.
the example heatmap
You can use DivergingNorm to specify an off-centered normalization. To create the cmap with black in the center, use LinearSegmentedColormap
from matplotlib.colors import LinearSegmentedColormap, DivergingNorm
cmap = LinearSegmentedColormap.from_list('BkR',['blue','black','red'])
norm = DivergingNorm(vmin=0, vcenter=70, vmax=100)
x,y = np.random.randint(0,100, size=(2,50))
plt.figure()
plt.scatter(x,y,c=y, norm=norm, cmap=cmap)
plt.colorbar()
plt.show()
I created a pie chart using matplotlib and I'd like to change the default colors to more softer colors, such as the hex RGB or RGBA string colors. I have the below script so far:
colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99']
explode = ((0.05,)*(len(annotation_df.index)))
fig1, ax1 = plt.subplots()
ax1.pie(annotation_df['count'], labels=annotation_df['annotation'], autopct='%1.1f%%', startangle=90, pctdistance=0.85, explode=explode,colors=colors) #colors=colors,
# draw circle
centre_circle = plt.Circle((0, 0), 0.70, fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
# Equal aspect ratio ensures that pie is drawn as a circle
ax1.axis('equal')
plt.tight_layout()
plt.show()
The problem is I need the colors to be set automatically, and I don't want specifically write the colors, as written above in the script.
Anyone knows how to do it?
You may define a color cycler to contain the colors you want to use.
import matplotlib.pyplot as plt
plt.rcParams['axes.prop_cycle'] = plt.cycler('color',
['#ff9999', '#66b3ff', '#99ff99', '#ffcc99'])
fig1, ax1 = plt.subplots()
ax1.pie([1,2,3], labels=list("ABC"), autopct='%1.1f%%')
ax1.axis('equal')
plt.tight_layout()
plt.show()
If you have less wedges than colors in the cycler only the those colors needed are used. If you have more wedges than colors in the cycler, they would be repeated. You can put as many colors as you like into the color cycler.
I am trying to create a pie chart, as follows:
import matplotlib.pyplot as plt
import pandas as pd
# make a square figure and axes
plt.figure(1, figsize=(10,10))
plt.axes([0.01, 0.1, 0.6, 0.6])
# plt.style.use('fivethirtyeight')
# The slices will be ordered and plotted counter-clockwise.
labels = 'foo1', 'foo2', 'foo3', 'foo4'
fracs = pd.Series([10,30, 50,10],index=labels)
fracs.plot(kind='pie', labels=None, autopct='%1.0f%%')
plt.legend(bbox_to_anchor=(0.95, .9), loc=2, borderaxespad=0.,labels=labels)
plt.title('pie chart demo which should be center aligned not left', bbox={'facecolor':'0.8', 'pad':5})
plt.show()
Which is yeilding a piechart as:
But, I am facing two problem:
1) I dont like the color scheme. I would like a color scheme more inline with (I need 12 colors)
2) Titel is centered at the pie chart only. The legend is somehow out. I am trying to get the title centered over the chart and the legend.
Can someone kindly help?
I think that is a ggplot colorscheme that you are trying to emulate.
And your plt.axes command is what is displacing your chart to the left.
Try this:
import matplotlib.pyplot as plt
plt.style.use('ggplot')
plt.figure(1, figsize=(10,10))
labels = 'foo1', 'foo2', 'foo3', 'foo4'
sizes = [10,30, 50,10]
plt.pie(sizes, labels=labels)
plt.show()
I would like to place a text box directly over a line plot that would cause the line at that point to 'fade', for legibility. My first thought is to change alpha of the text box. However that requires me to set a background colour, which I do not want (the plot background is currently transparent, which I would like to keep). There may be other elements of various shapes/colours that would be similarly stacked under the text. Ideally, what I want is a semi-opaque text box that has no colour of its own, but which would cause elements underneath the text box to be half-hidden. Alpha seems to be not the way to go, but is there another attribute that I can modify?
import matplotlib.pyplot as plt
import pandas as pd
pd.set_option("display.mpl_style", 'default')
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ax1.plot((0,1),(0.5,0.5), "--",lw=2);
ax1.plot((0,0.25,0.5,0.75,1),(0,0.25,0.5,0.75,1), "go--",markersize=20)
ax1.text(0.25, 0.5, "HardToRead", fontsize=12,ha="center",va="center", bbox=dict(boxstyle="square,pad=0.1", fc='white', ec="white", lw=1,alpha=0.1))
ax1.text(0.75, 0.5, "EasyToRead", fontsize=12,ha="center",va="center", bbox=dict(boxstyle="square,pad=0.1", fc='white', ec="white", lw=1,alpha=0.9))
ax1.text(0.5, 0.5, "CrossTalk", fontsize=12,ha="center",va="center", rotation=90, bbox=dict(boxstyle="square,pad=0.1", fc='white', ec="white", lw=1,alpha=0.6))