MPAndroidChart LineChart begin at the last x value - android-studio

I'm trying to use moveViewToX to show a chart starting from the last x value, so the user drags the chart to see older data. But it does not move exactly where I expected, I'm moving to the value 2.013 but it's starting before it, then I can't see my last x value which is 2.018. Take a look at the images for better understanding.
[image showing what's happening] [1]: https://i.stack.imgur.com/t0Z4f.png
[image showing what I expected to happen][2]: https://i.stack.imgur.com/U3JhM.png
Here's the part of the code that modifies LineChart
mLineChart.getAxisRight().setEnabled(false);
mLineChart.getDescription().setEnabled(false);
mLineChart.getXAxis().setAvoidFirstLastClipping(true);
mLineChart.setVisibleXRangeMaximum(5);
mLineChart.getXAxis().setGranularity(1);
mLineChart.moveViewToX(2013);
mLineChart.invalidate();
I'd appreaciate any help. Thanks!

You need to set a dragOffsetX, otherwise the chart will not allow you to move past the end of the last value on the x axis.
You can also try specifying the axis maximum:
mLineChart.getXAxis().setAxisMaximum(lastValue + 5)

Related

Displaying multiple values in Altair/Streamlit tooltips on a bar chart

My DataFrame looks similar to this:
name
reached points
Jose Laderman
13
William Kane
13
I am currently displaying the aggregated count of students reached points of an assignment on an Altair bar chart within Streamlit like this:
brush = alt.selection(type='interval', encodings=['x'])
interactive_test = alt.Chart(df_display_all).mark_bar(opacity=1, width=5).encode(
x= alt.X('reached points', scale=alt.Scale(domain=[0, maxPoints])),
y=alt.Y('count()', type='quantitative', axis=alt.Axis(tickMinStep=1), title='student count'),
).properties(width=1200)
upper = interactive_test.encode(
alt.X('reached points', sort=alt.EncodingSortField(op='count', order='ascending'), scale=alt.Scale(domain=brush, domainMin=-0.5))
)
lower = interactive_test.properties(
height=60
).add_selection(brush)
concat_distribution_interactive = alt.vconcat(upper, lower)
Which produces this output and everything looks fine
The information I want my tooltip to show is a list of students that reached the specific amounts of reached points I'm hovering over. When adding something like:
tooltip='name'
the way my bar chart seems to display values has now been altered to this
When adding something like
tooltip='reached points'
The data seems to be displayed normally but without a tooltip that gives me the necessary information. Is it possible to display tooltip data that isn't used in my x or y axis but still part of the DataFrame I'm putting into the chart?

How to delete multiple plot axes made with subplot on scilab

how are you. I've all day trying to do the following: I'm designing a PID controller and depending on how much data I want to analize then I show only one plot or four plots in the same figure (this last multiple plot is made with subplot).
All the calculation and plots are made in one function with options in function dependind on what I want to see, I have several buttons and text inputs in the application that I'm coding and right know the PID design and the simple or multiple plots are working; my current program is that the axes handle is being assigned to local variables inside the function and of course this variables are distroyed when the program exits the function.
A more detailed explanation of the problem is the following: if I select multiple plot and then I want one plot then I don't know how to delete the four axes to clean the figure without closing it, so when I want to activate the simple plot then the program only erases one of the four plots and prints the simple plot over the remaining three while the remaining three subplots remains visible. Following is the piece of code that is giving me problems:
grafico=sistem_graf;
disp(grafico);
if(isdef('aa')==%T);
scf(aa);
delete(get("current_axes"));
disp("aa");
end
if(isdef('bb')==%T);
scf(bb);
delete(get("current_axes"));
disp("bb");
end
if(isdef('cc')==%T);
scf(cc);
delete(get("current_axes"));
disp("cc");
end
if(isdef('dd')==%T);
scf(dd);
delete(get("current_axes"));
disp("dd");
end
select ventana
case 0
tipoG=0;
plot2d(T,graffta);
xtitle('Ω(S)/R(S): Respuesta al escalón (Sistema original)','t [s]','Vel [rpm]');
legend(['Original';'C/control P';'C/Control PI';'C/Control PID']);
aa=get("current_axes");
aa.axes_bounds = [1/3 0 2/3 1];
case 1
tipoG=1;
// delete(get("current_axes"));
plot(T,[graffta;sist_P_ZN;sist_PI_ZN;sist_PID_ZN]);
xtitle('Ω(S)/R(S): Respuesta al escalón','t [s]','Vel [rpm]');
legend(['Original';'C/control P';'C/Control PI';'C/Control PID']);
aa=get("current_axes");
aa.axes_bounds=[1/3 0 2/3 1];
case 2
tipoG=2;
//delete(get("current_axes"));
subplot(421);
plot(T,[graffta;sist_P_ZN;sist_PI_ZN;sist_PID_ZN]);
xtitle('Ω(S)/R(S): Respuesta al escalón','t [s]','Vel [rpm]');
legend(['Original';'C/control P';'C/Control PI';'C/Control PID']);
aa=get("current_axes");
aa.axes_bounds=[1/3,0,1/3,1/2]
subplot(422);
plot2d(T,(kt*kp/(j*la))*(1/(afta*bfta))*(1+(1/(afta-bfta))*(bfta*exp(-afta*T)-afta*exp(-bfta*T))));
xtitle('Ω(t)','t [s]','Vel [rpm]');
bb=get("current_axes");
bb.axes_bounds=[2/3,0,1/3,1/2];
subplot(423);
plot2d(T,(kt/(j*la))*(1/(afta*bfta))*((1/(afta-bfta))*(bfta*afta*exp(-bfta*T)-afta*bfta*exp(-afta*T))));
xtitle('Variación de velocidad angular dΩ/dt','t [s]','Ac. [rpm/s^2]');
cc=get("current_axes");
cc.axes_bounds=[1/3,1/2,1/3,1/2];
subplot(424);
plot(T,[sist_P_ZN;sist_PI_ZN;sist_PID_ZN]);
xtitle('Controladores','t [s]','Vel [rpm]');
legend(['C/control P';'C/Control PI';'C/Control PID']);
dd=get("current_axes");
dd.axes_bounds=[2/3,1/2,1/3,1/2];
end
the four if(isdef("variable_name")) never doesn't works because when the program enters the functions those variables doesn't exists yet, those are created after the plots with subplot.
If the graphics properties are displayed from the Edit menú of the figure then all the children axes are shown but if I try to get the axes with for example
a=get(sistem_graf.Axes(1))
then a console display is shown saying that the property "Axes" doesn't exist so I don't know how to solve the problem.
Please help!
Thanks in advance.
Update 03/06/2021:
I found a way to have the behavior I need an is working at 90%, it only fails when I go from detailed plot to simple plot (is erasing one graphic) but backwards it works fine, I changed all the code before "Select ventana" to the following
if(imps(1) ~= -1)
if(size(imps) == 1)
sca(imps(1));
disp(imps(1));
delete(get("current_axes"));
elseif(size(imps) > 1)
sca(imps(1));
delete(get("current_axes"));
sca(imps(2));
delete(get("current_axes"));
sca(imps(3));
delete(get("current_axes"));
sca(imps(4));
delete(get("current_axes"));
end
end
and filled an array whose content is all the axes ploted last time that the function was called, that array is returned from the function to a global variable and finally that global variable is feed to the function in the next call and assigned to the local variable "imps".
I think is some kind of crazy but apparently scilab doesn't have incorpotated the static variable aproach.
If I have understood your problem, the small example below should help you:
function fun(nb)
ch = gcf().Children;
delete(ch(ch.type=="Axes"))
t = linspace(0,1,100);
if nb==1
plot(t,t)
else
for i=1:nb
subplot(2,2,i)
plot(t,t.^i)
end
end
endfunction
clf
uicontrol("style","pushbutton","units","normalized","Position",[0.1 0.1 0.4 0.1],"string","one plot","callback","fun(1)");
uicontrol("style","pushbutton","units","normalized","Position",[0.5 0.1 0.4 0.1],"string","four plots","callback","fun(4)");
I think that the mechanism you were missing is the following:
ch = gcf().Children;
delete(ch(ch.type=="Axes"))
i.e. something that allows to delete the plot axes and keep the uicontrols alive. The above construct uses logical indexing. Here ch.type=="Axes" is a vector of boolean values with components i egal to true when the type field of corresponding component ch(i) is Axes. Then, ch(ch.type=="Axes") is the subset of components of ch such that the boolean index has value true. Hence, at last, the subset of components of ch which are of Axes type.

Ticks on color bar are overlapping because the values are very close to each other

I'm trying to display the exact values on one axis of the color bar and a basic scale on the other. However, some of the exact values are so close together their names overlap on the color bar. Is there a way for me to make the overlapping names appear as a list or just to the side the other values name? I've already tried rotation of the labels, setting vmin/vmax in the color bar method, and setting the ylim's of the second axis. I'm at a lose at what to try next. It feels like this is something matplotlib would allow but I can't find what method or kwargs that allow this manipulation. Many of the commented out tlines are the attempts I've made with help from many posts on StackOverflow. Thank you!!
Previous code deleted for clarity
UPDATE: Paul H here is a workable example with the same issue I'm trying to fix
# Make random data with same issue
x, y = np.linspace(-3, 1.5, 20), np.linspace(0, 0.5, 20)
# two different ranges used to simulate the same issue in my data
fake_phase = np.append(np.random.random_sample(15), np.arange(0.0, .005, 0.001))
fake_labels = np.array(['V439Oph', 'ALVir', 'YZVir', 'XXVir', 'V716Oph', 'BFSer', 'BLHer',
'RXLib', 'CEHer', 'V465Oph', 'V1180Sgr', 'CSCas', 'DQAnd', 'IXCas',
'UYEri', 'TWCap', 'AUPeg', 'MZCyg', 'SWTau', 'TXDel'], dtype=object)
# Plot data
fig, ax = plt.subplots(1,1,figsize=(15,10))
plt.tight_layout()
plt.plot(x, y, marker='.', ms=17, mew=2, linestyle='none')
# Make the same colorbar
norm = cm.colors.Normalize(vmin=0.0, vmax=1.0, clip=False)
cbar = fig.colorbar(cm.ScalarMappable(norm=norm, cmap='rainbow'), ax=ax, extend='both',
orientation='vertical', pad=0.005, use_gridspec=True)
cbar.set_ticks(fake_phase)
cbar.set_ticklabels(fake_labels)
cbar.ax.tick_params(which='major', labelsize='large', width=1.5, length=6)
cbar.set_label(label='Phase', size='xx-large', labelpad=40)
cbar.ax.set_aspect('auto')
ax2 = cbar.ax.twinx()
pos = cbar.ax.get_position()
pos.x0 += 0.1
ax2.set_position(pos)
plt.show();
The output of this code: Output of workable example
My issue is that the secondary axis on the colorbar (left axis) has values that are so close together their labels overlap. I'm hoping to find a way to space the labels so they are readable. I thought I found a way to accomplish this using axis.set_ticklabels() (set_ticklabels() documentation. In the **kargs section of the doc it references using text properties. In the text properties documentation text properties doc the property 'y' says you can set the y-position of the text. However, when I add this keyword to set_ticklabels() I get an error that the keyword is not recognized.. I've tried adding the property 'y' as a keyword and attribute but I get a keyword error or does not have that attribute error...
I'm calling the property wrong but I've never gotten this detailed in editing these parameters. I honestly don't know if this is the best way to solve this, but it's the closest I've gotten so far. I was hoping to use it to offset the labels so they were stacked vertically on top of each other in the same order but far enough apart that the label is readable.
Thanks for any input!

Python Docx Table row height

So column width is done using cell width on all cells in one column ike this:
from docx import Document
from docx.shared import Cm
file = /path/to/file/
doc = Document(file)
table = doc.add_table(4,2)
for cell in table.columns[0].cells:
cell.width = Cm(1.85)
however, the row height is done using rows, but I can't remember how I did it last week.
Now I managed to find a way to reference the rows in a table, but can't seem to get back to that way. It is possible to change the height by using the add_row method, but you can't create a table with no rows, so the the top row will always be the default height, which about 1.6cms.
There is a way to access paragraphs without using add_paragraph, does anyone know how to access the rows without using the add_row method because it was that that I used to set row height in a table as a default.
I have tried this but it doesn't work:
row = table.rows
row.height = Cm(0.7)
but although this does not give an error, it also has no effect on the height.
table.rows is a collection, in particular a sequence, so you need to access each row separately:
for row in table.rows:
row.height = Cm(0.7)
Also check out row.height_rule for some related behaviors you have access to:
https://python-docx.readthedocs.io/en/latest/api/table.html#row-objects
When you assign to table.rows.height, it just adds a .height instance attribute that does nothing. It's one of the side-effects of a dynamic language like Python that you encounter a mysterious behavior like this. It goes away as you gain more experience though, at least it has for me :)
Some additional information:
The answer here is correct, but this will give a minimum row height. Using WD_ROW_HEIGHT_RULE.EXACTLY will fix the cell height to the set row height ignoring the contents of the cell, this can result in cropping of the text in an undesirable way.
para = table.cell(0,0).add_paragrph('some text')
SOLUTION:
add_paragraph actually adds a blank line above the text.
Use the following instead to avoid using add_paragraph:
table.cell(0,0).paragraphs[0].text = 'some text'
or using add_run can make it easier to also work with the text:
run = table.cell(0,0).paragraphs[0].add_run('some text')
run.bold = True

Setting multiple axvspan labels as one element in legend

I am trying to set up a series of vertical axis spans to symbolize different switching positions at different times. For example, in the figure below, switching position 1 (green) happens quite a few times, alternating between other positions.
I plot these spans running a for loop in a list of tuples, each containing the initial and final indexes of each interval to plot the axvspan.
def plotShades(timestamp, intervals, colour):
for i in range(len(intervals)):
md.plt.axvspan(timestamp[intervals[i][0]], timestamp[intervals[i][1]], alpha=0.5, color=colour, label="interval")
This function is then called upon another one, that plots the shades for each different switching position:
def plotAllOutcomes(timestamp, switches):
#switches is a list of 7 arrays indicating when the switcher is at each one of the 7 positions. If the array has a 1 value, the switcher is there. 0 otherwise.
colors = ['#d73027', '#fc8d59', '#fee08b', '#ffffbf', '#d9ef8b', '#91cf60', '#1a9850']
intervals = []
for i in range(len(switches)):
intervals.append(getIntervals(switches[i], timestamp))
plotShades(timestamp, intervals[i], colors[i])
md.plt.legend()
Doing so with the code snippets I've put here (not the best code, I know - I'm fairly new in Python!) the legend ends up having one item for each interval, and that's pretty awful. This is how it looks:
I'd like to get a legend with only 7 items, each for a single color in my plot of axvspans. How can I proceed to do so? I've searched quite extensively but haven't managed to find this situation being asked before. Thank you in advance for any help!!
A small trick you can apply using the fact that labels starting with "_" are ignored:
plt.axvspan( ... , label = "_"*i + "interval")
Thereby a label is only created for the case where i==0.

Resources