In a Python Plotly bar chart with two y-axes, how do I add horizontal lines using values from both y-axes? - plotly-python

I created a graph object bar chart that has two different y-axes so that I can chart two different of different units for each category. However, now I'd like to add two horizontal lines. They won't be using values from the same y-axis though.
fig = go.Figure(
data=[
go.Bar(name='Bar 1',
x=category_x,
y=measure_1,
yaxis='y',
offsetgroup=1),
go.Bar(name='Bar 2',
x=category_x,
y=measure_2,
yaxis='y2',
offsetgroup=2)
]
)
Things look fine up to here.
Now, I'd like to add one horizontal line based off values from the first y-axis and another horizontal line based off values from the second y-axis. I tried using fig.add_hline() for this, but then both horizontal lines are displayed using values from the first y-axis. What should I be doing instead?

Related

problem in reordering the graph axis ggplot2, phyloseq

i have a shiny appp created which plots metagenome data using ggplot2, phyloseq and plotly with dplyr and tidyr. It creates pretty good stacked barplots and heatmaps only problem is it reorders sample names at x-axis e.g. 1-10 are arranged as 1,10,2,,5,6... how to correct that bug?

is there a way to plot multiple lines using hvplot.line from an xarray array

I have multiple ytraces data in an xarray array.
data trace selection can be done by
t=s_xr_all.sel(trace_index=slice(0,2,1),xy='y')
# trace_index and xy are dimension names and above selects subset of 3 traces (lines) into t
t.name='t'
t.hvplot.line(x='point_index',y='t')
The above creates a line plot with a widget slider that allows scrolling through the lines with single line displayed at a time
I would like to be able to plot all lines without creating the slider widget.hvplot documentation is sparse as to how to do that
t.hvplot.line(x='point_index',y='t').overlay()
The .overlay() function chaining eliminates the slider creation and all the lines in the xarray are displayed

TramineR legend position and axis

I'm working with TraMineR and I don't know how to arrange my plot. So basically what i would like to have the legend under the plot and to remove the space between the x and y axis. Any help is welcomed.
The plot:
Sample code:
seqdplot(Activities.seq, with.legend=FALSE)
legend("bottom", legend=attr(Activities.seq, "labels"),
fill=attr(Activities.seq, "cpal"),
inset=-.1, bty="o", xpd=NA, cex=.75,ncol=3)
The family of seqplot functions offers a series of arguments to control the legend as well as the axes. Look at the help page of seqplot (and of plot.stslist.statd for specific seqdplot parameters).
For instance, you can suppress the x-axis with axes=FALSE, and the y-axis with yaxis=FALSE.
To print the legend you can let seqdplot display it automatically using the default with.legend=TRUE option and control it with for examples cex.legend for the font size, ltext for the text. You can also use the ncol argument to set the number of columns in the legend.
The seqplot functions use by default layout to organize the graphic area between the plots and the legend. If you need more fine tuning (e.g. to change the default par(mar=c(5.1,4.1,4.1,2.1)) margins around the plot and the legend), you should create separately the plot(s) and the legend and then organize them yourself using e.g. layout or par(mfrow=...). In that case, the separate graphics should be created by setting with.legend=FALSE, which prevents the display of the legend and disables the automatic use of layout.
The color legend is easiest obtained with seqlegend.
I illustrate with the mvad data that ships with TraMineR. First the default plot with the legend. Note the use of border=NA to suppress the too many vertical black lines.
library(TraMineR)
data(mvad)
mvad.scode <- c("EM", "FE", "HE", "JL", "SC", "TR")
mvad.seq <- seqdef(mvad, 17:86,
states = mvad.scode,
xtstep = 6)
# Default plot with the legend,
seqdplot(mvad.seq, border=NA)
Now, we suppress the x and y axes and modify the display of the legend
seqdplot(mvad.seq, border=NA,
axes=FALSE, yaxis=FALSE, ylab="",
cex.legend=1.3, ncol=6, legend.prop=.11)
Here is how you can control the space between the plot and the x and y axes
seqdplot(mvad.seq, border=NA, yaxis=FALSE, xaxis=FALSE, with.legend=FALSE)
axis(2, line=-1)
axis(1, line=0)
Creating the legend separately and reducing the left, top, and right margins around the legend
op <- par(mar=c(5.1,0.1,0.1,0.1))
seqlegend(mvad.seq, ncol=2, cex=2)
par(op)

Matplotlib stacked bar chart not showing all bars

I will make a stacked bar chart in matplotlib. Somehow it doesnt include all the bar chart that i gave him (there should be like 50 bar charts stacked on each other)
The code:
N=45 #numbers of columns
max_el=50
ind=np.arrange(N)
for bar in range(0,max_el):
y=[dic[value][bar] for value in dic]
plt.bar(ind,y,)
plt.show()
note: I used the similar code and same data and made a stacked bar chart with plotly (which worked)
With plotly
With matplotlib
Some of the values of variables are zeros or 0.1. Could that be the problem ?
As described in the comments, you need to add a bottoms array that keeps track of how much each should be moved up from the 0 line. Otherwise, they all start plotting a 0 and overplot one another, with the tallest one sticking up to its values and each one hiding those that were plotted before.

How to create a scatter plot legend with only one symbol for each label?

How can I create a scatter plot legend without two symbols showing up each time? I can understand why you'd want this when you're joining symbols by lines, but for a pure scatter plot, all I want in the legend is one example of the symbol. This plot from a previous stackoverflow post shows the kind of thing I mean:
In the legend command you can use the scatterpoints option:
ax.legend(loc=0, scatterpoints = 1)
For a normal plot, it is the option numpoints.
Here you can find more information about the keyword arguments for the legend: http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.legend