How can I change textposition on plotly based on the information? - pandas

I have this code:
data = go.Scatter(
x=positionsX,
y=positionsY,
textposition='middle center',
mode='markers+text',
marker=dict(
color=color,
opacity=[1, 1, 1, 1, 1],
size=[100, 70, 60, 30, 25]),
text=list(sumByRegion.keys()),
)
and I wanna change the textposition to 'bottom left' based on sumByRegion.keys() value.
sumByRegion.keys() is dict_values([55, 24, 16, 3, 2])
What I have now is on the image:
image
edit:
Actually, I was looking for set the textposition for each individual item. Therefore, I used a array of textposition to fix the problem.
data = go.Scatter(
x=positionsX,
y=positionsY,
textposition=["middle center", "middle center", "middle center", "middle right", "middle left"],
mode='markers+text',
marker=dict(
color=color,
opacity=[1, 1, 1, 1, 1],
size=[100, 70, 60, 30, 25]),
text=list(sumByRegion.keys()),
)

If the text you want to display is in dictionary format, then you can convert it to the format you want to display. I'm not sure what the desired format is, but I added a line break and added a percent sign.
import plotly.graph_objects as go
sumByRegion = {'USA':55,'EU':24,'Asia':16,'Africa':2,'South America':3}
text = {'{}<br>{}%'.format(k,v) for k,v in zip(sumByRegion.keys(), sumByRegion.values())}
data = go.Scatter(
x=positionsX,
y=positionsY,
textposition='bottom left',
mode='markers+text',
marker=dict(
color=color,
opacity=[1, 1, 1, 1, 1],
size=[100, 70, 60, 30, 25]),
text=text,
)

Related

How to reverse colorbar values in matpotlib?

I am using the cbar.ax.tick_params matplotlib command to make a colorbar for an XY scatterplot. How do I reverse the values (not the color-ramp) so that the lowest value is at the top of the bar. This is to represent geological data where the youngest rocks are on top of the older rocks. Here the age is represented by color.
Here is my code:
plt.scatter(summary["d18O"], summary["eHf"], s=150, c = color, cmap = color_map, edgecolors='black', marker='o')
plt.errorbar(summary["d18O"], summary["eHf"], summary["xerr"], summary["yerr"], ls='none', color='lightgrey', zorder=-1)
cbar=plt.colorbar()
cbar.ax.tick_params(labelsize=14)
cbar.minorticks_on()
cbar.set_label('Age (Ma)', style='italic', fontsize=16)
plt.axvline(x=5.3, color='black', zorder=-1)
plt.axhline(y=0, color='black', zorder=-1)
plt.tick_params(labelsize=14)
ax.set_xticks([4, 5, 6, 7, 8, 9, 10, 11, 12, 13])
ax.set_yticks([-6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16])
plt.ylabel(u'${\epsilon}$Hf$_{T}$', style='italic', fontsize=18)
plt.xlabel(u'$\delta^{18}$O$_{V-SMOW}$ ‰',style='italic', fontsize=18)
plt.text(11.5, 0.3, 'CHUR', fontsize=18)
plt.text(4.9, 5, 'mantle zircon = 5.3‰', fontsize=16, rotation=90)
plt.show()
As #r-beginners mentioned,
cbar.ax.invert_yaxis()
would solve the problem if cbar is your colorer object.

Numpy : How to assign directly a subarray from values when these values are step spaced

I have 2 global arrays "tab1" and "tab2" with dimensions respectively equal to 21x21 and 17x17.
I would like to assign the block of "tab1" ( indexed by [15:20,0:7]) by the block of "tab2" indexed by [7:17:2,0:7] (so with a step between elements of 1st array dimension) : I tried whith this syntax :
tab1[15:20,0:7] = tab2[7:17:2,0:7]
Unfortunately, this doesn't work, it seems that only "diagonal" (I mean one by one) elements of 15:20 are taken into account following the values of "tab2" along [7:17:2].
Is there a way to assign a subarray of "tab1" with another subarray "tab2" composed of indexes with step spaced values ?
If someone could see what's wrong or suggest another method, this would be nice.
UPDATE 1: indeed, from my last tests, it seems good but is it also the same for the assignment of block [15:20,15:20] :
tab1[15:20,15:20] = tab2[7:17:2,7:17:2]
??
ANSWER : it seems ok also for this block assignment, sorry
The assignment works as I expect.
In [1]: arr = np.ones((20,10),int)
The two blocks have the same shape:
In [2]: arr[15:20, 0:7].shape
Out[2]: (5, 7)
In [3]: arr[7:17:2, 0:7].shape
Out[3]: (5, 7)
and assigning something interesting, looks right:
In [4]: arr2 = np.arange(200).reshape(20,10)
In [5]: arr[15:20, 0:7] = arr2[7:17:2, 0:7]
In [6]: arr
Out[6]:
array([[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
...
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[ 70, 71, 72, 73, 74, 75, 76, 1, 1, 1],
[ 90, 91, 92, 93, 94, 95, 96, 1, 1, 1],
[110, 111, 112, 113, 114, 115, 116, 1, 1, 1],
[130, 131, 132, 133, 134, 135, 136, 1, 1, 1],
[150, 151, 152, 153, 154, 155, 156, 1, 1, 1]])
I see a (5,7) block of values from arr2, skipping rows like [80, 100,...]

matplotlib histogram with equal bars width

I use a histogram to display the distribution. Everything works fine if the spacing of the bins is uniform. But if the interval is different, then the bar width is appropriate (as expected). Is there a way to set the width of the bar independent of the size of the bins ?
This is what i have
This what i trying to draw
from matplotlib import pyplot as plt
my_bins = [10, 20, 30, 40, 50, 120]
my_data = [5, 5, 6, 8, 9, 15, 25, 27, 33, 45, 46, 48, 49, 111, 113]
fig1 = plt.figure()
ax1 = fig1.add_subplot(121)
ax1.set_xticks(my_bins)
ax1.hist(my_data, my_bins, histtype='bar', rwidth=0.9,)
fig1.show()
I cannot mark your question as a duplicate, but I think my answer to this question might be what you are looking for?
I'm not sure how you'll make sense of the result, but you can use numpy.histogram to calculate the height of your bars, then plot those directly against an arbitrary x-scale.
x = np.random.normal(loc=50, scale=200, size=(2000,))
bins = [0,1,10,20,30,40,50,75,100]
fig = plt.figure()
ax = fig.add_subplot(211)
ax.hist(x, bins=bins, edgecolor='k')
ax = fig.add_subplot(212)
h,e = np.histogram(x, bins=bins)
ax.bar(range(len(bins)-1),h, width=1, edgecolor='k')
EDIT Here's with the adjustment to the x-tick labels so that the correspondence is easier to see.
my_bins = [10, 20, 30, 40, 50, 120]
my_data = [5, 5, 6, 8, 9, 15, 25, 27, 33, 45, 46, 48, 49, 111, 113]
fig = plt.figure()
ax = fig.add_subplot(211)
ax.hist(my_data, bins=my_bins, edgecolor='k')
ax = fig.add_subplot(212)
h,e = np.histogram(my_data, bins=my_bins)
ax.bar(range(len(my_bins)-1),h, width=1, edgecolor='k')
ax.set_xticks(range(len(my_bins)-1))
ax.set_xticklabels(my_bins[:-1])

Fuzzy logic controller - RuntimeError: Unable to resolve rule execution order

I am new to this concept and i have been trying to implement an fuzzy logic controller for shower. the input are the postion of knob from extreme left to extreme right and outputs are tempreture from very cold to very hot. i am encountering this Runtime error in the rules. Below is my stupid code
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
pos = ctrl.Consequent(np.arange(0, 180, 1), 'pos')
temp = ctrl.Consequent(np.arange(0, 100, 1), 'temp')
pos['EL'] = fuzz.trimf(pos.universe, [0, 0, 45])
pos['L'] = fuzz.trimf(pos.universe, [0, 45, 90])
pos['C'] = fuzz.trimf(pos.universe, [45, 90, 135])
pos['R'] = fuzz.trimf(pos.universe, [90, 135, 180])
pos['ER'] = fuzz.trimf(pos.universe, [135, 180, 180])
temp['VC'] = fuzz.trimf(temp.universe, [0, 0, 10])
temp['C'] = fuzz.trimf(temp.universe, [0, 10, 40])
temp['W'] = fuzz.trimf(temp.universe, [10, 40, 80])
temp['H'] = fuzz.trimf(temp.universe, [40, 80, 100])
temp['VH'] = fuzz.trimf(temp.universe, [80, 100, 100])
rule1 = ctrl.Rule(pos['EL'], temp['VC'])
rule2 = ctrl.Rule(pos['L'], temp['C'])
rule3 = ctrl.Rule(pos['C'], temp['W'])
rule4 = ctrl.Rule(pos['R'], temp['H'])
rule5 = ctrl.Rule(pos['ER'], temp['VH'])
temp_ctrl = ctrl.ControlSystem([rule1, rule2, rule3, rule4, rule5])
temprature = ctrl.ControlSystemSimulation(temp_ctrl)
RuntimeError: Unable to resolve rule execution order. The most likely reason is two or more rules that depend on each other. Please check the rule graph for loops.
I think you might want this
pos = ctrl.Consequent(np.arange(0, 180, 1), 'pos')
to be this
pos = ctrl.Antecedent(np.arange(0, 180, 1), 'pos')
so your rules will read something like
if antecedent then consequent

Filter sequence items in TensorFlow

I have a tensor of allowed items
index = tf.constant([61, 215, 23, 18, 241, 125])
and need to remove items from input sequence batches that are not in index.
seq = tf.constant(
[
[ 18, 241, 0, 0],
[125, 61, 23, 241],
[ 23, 92, 18, 0],
[ 5, 61, 215, 18],
]
)
After the calculation in this case I need
result_needed = tf.constant(
[
[ 18, 241, 0, 0],
[125, 61, 23, 241],
[ 23, 18, 0, 0],
[ 61, 215, 18, 0],
]
)
I cannot do this in Python because this calculation happens during predictions. Also note that while item IDs here are small, solution needs to deal with numbers from 1 to 2^40.
Answer
After some serious pondering time, I came up with the following:
idx_range = tf.reshape(tf.range(seq.shape[-2]), [-1, 1])
idx_tile = tf.tile(idx_range, [1, seq.shape[-2].value])
idx_flat = tf.reshape(idx_tile, [-1])
truth_value = tf.equal(index, tf.expand_dims(seq, -1))
one_hot = tf.to_float(truth_value)
ones = tf.nn.top_k(tf.reduce_sum(one_hot, -1), seq.shape[-1]).indices
ones_flat = tf.reshape(ones, [-1])
ones_idx = tf.reshape(
tf.stack([idx_flat, ones_flat], axis=1),
tf.concat([seq.shape, [2]], axis=0)
)
tf.gather_nd(seq, ones_idx)
This is not exactly what I said I needed, but actually got me close enough. Instead of the output replacing the blacklisted items with 0, it moves them to the end. If you needed them gone, I'm sure there's a method to remove them, but I'm not looking into it. Apologies.