Creating a plotly vrect over all axis of a matplotlib plot - python

I have a complete matplotlib figure with multiple subplots and I want to add a vertical span in a specific range to all of the subplots. The remaining plot is then converted to plotly.
If I try running xvspan over each individual subplot when creating the matplotlib figure, the vertical spans do not make it into the plotly figure.
If I try converting the figure into plotly first and then adding a vrect, this vrect only seems to work for the first subplot. I have tried using row = "all" to no avail.
Any other ideas?
Thanks!

Related

Plotting pandas vs matplotlib

For a little jupyter notebook widget i tried the following:
with output:
fig, ax = plt.subplots(constrained_layout=True, figsize=(6, 4))
# move the toolbar to the bottom
fig.canvas.toolbar_position = 'bottom'
ax.grid(True)
line, = ax.plot(df.index, init_data, initial_color,)
ax.set_ylabel('CH4_HYD1')
ax.set_ylabel('')
But the data gets plotted in a wierd way. When isolating the plotting method and comparing it to the pandas default, the pandas plot displays the data correctly.
df['CH4_HYD1'].plot()
Pandas plot
plt.plot(['CH4_HYD1'])
Wrong plot?
I would like to use the axis plot method to produce a plot with multiple lines of different features. But I can't find the probably very simple error... What is the difference here?
This is a short example of how to plot multiple lines from a DataFrame with matplotlib and with pandas. Essentially, pandas uses matplotlib to do the plots so it shouldn't be different.
Anyway, I encourage you to look into seaborn. This is a great library based on pandas and matplotlib that enables visualizing dataframes easily.

Matplotlib don't show all ticks/labels with bar chart?

When plotting a line with many points, matplotlib automatically hides some x-axis ticks/labels to make sure all labels can fit.
If I want to plot a bar chat with many points, is there anyway to make matplotlib think of the x-axis in the same as with a line plot? Right now it's trying to squeeze in all the labels.
I would suggest specifiying the list of xtick labels :
plt.xticks([80,150,300]) ### In this case these are numbers, change them to your values

shared_xaxis with plotly, but can I still display the axis on each plot?

if you share the x_axis between 2 plots, they will get aligned, as expected.
however, the axis is displayed only once, on a single plot and not on both anymore.
Is it possible to display the axis on each plot?
If you're using Plotly Express, there is no way to do this at the moment. If you're using the underlying make_subplots method, however, this is the default behaviour: https://plot.ly/python/subplots/

What is the difference between axes and pyplot

I am trying to embed matplotlib figure into PyQt4, but when I try to plot the figure using pyplot, it's giving an error or would make a separate matplotlib built-in figure object rather than plotting into my canvas.
If I try to plot using axes, all is good as figure gets plotted into the canvas. However, I can't plot two axes into a single graph which was done by pyplot by simply plotting two plots one after another.
So how can I 2 plots into one graph using axes?
class Foo(blah):
""" some code here """
# code which giving error:
self.pyplot.plot_date(x,y)
self.pyplot.plot_date(a,b)
# code plotting matplotlib figure object rather drawing into canvas
pyplot.plot_date(x,y)
pyplot.plot_date(a,b)
# code only plotting only a,b - but not x,y
self.axes.plot_date(x,y)
self.axes.plot_date(a,b)

show two plot together in Matplotlib like show(fig1,fig2)

In general, I don't have any problem to put two plots in a figure like plot(a);plot(b) in matplotlib. Now I am using a particular library which would generate a figure and I want to overlay with boxplot. Both are generated by matplotlib. So I think it should be fine but I can only see one plot. Here is the code. I am using beeswarm and here is its ipython notebook. I can only plot beeswarm or boxplot but not both in a figure. My main goal is trying to save column scatter plot and boxplot together as a figure in pdf. Thanks,
from beeswarm import beeswarm
fig=plt.figure()
figure(figsize=(5,7))
ax1=plt.subplot(111)
fig.ylim=(0,11)
d2 = np.random.random_integers(10,size=100)
beeswarm(d2,col="red",method="swarm",ax=ax1,ylim=(0,11))
boxplot(d2)
The problem is with the positioning of the box plot. The default positioning list starts with 1 what shifts the plot to 1 and your beeswarm plot sits on 0.
So the plots are in different places of your canvas.
I modified your code a little bit and that seems to solve your problem.
from beeswarm import beeswarm
fig = plt.figure(figsize=(5,7))
ax1 = fig.add_subplot(111)
# Here you may want to use ax1.set_ylim(0,11) instead fig.ylim=(0,11)
ax1.set_ylim(0,11)
d2 = np.random.random_integers(10,size=100)
beeswarm(d2,col="red",method="swarm",ax=ax1,ylim=(0,11))
boxplot(d2,positions=[0])
Cheers

Categories

Resources