I have several DF with only numerical columns. I'm using df.plot.line() to see the data and it works fine as it plots graphs separately.
Now I'm trying a way to arrange those graphs into a subplot but I can't find a way.
here is an example:
df_km_cumsum.plot.line(figsize=[20,15],legend=False)
gives this plot
df_daytot_km.plot.line(figsize=[20,15],legend=False)
gives this other plot
Now I'd like to put them inside a 2x1 figure to make them see together.
Any help is kindly accepted.
Thank you
plt.subplot(2, 1, 1)
df_km_cumsum.plot.line(figsize=[20,15],legend=False)
plt.subplot(2, 1, 2)
df_daytot_km.plot.line(figsize=[20,15],legend=False)
Thanks to #tmdavision, here is the final solution:
fig, (ax1, ax2) = plt.subplots(2,figsize=[20,15])
df_km_cumsum.plot.line(legend=False, ax=ax1)
df_dayavg_km.plot.line(legend=False, ax=ax2)
plt.show()
Related
I am implementing an algorithme with Python and I would like to plot 2 subplots.
This is what I obtain:
I would like to know if there is a way to automaticaly adjust the space between the two plots to see the title & the xlabel?
Thanks
I don't know how to do it.
You have to use plt.tight_layout:
fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)
ax1.plot(arr.real)
ax1.set_title('Real part at the output of the filter')
ax2.plot(arr.imag)
ax2.set_title('Imaginary part at the output of the filter')
plt.tight_layout()
plt.show()
With plt.tight_layout:
Without plt.tight_layout:
I have a function named make_plot that takes as an input a list of matrices of length 15 and outputs a plot that is a 3x5 collection of heatmaps (one for each of the matrices in the list).
I would then like to display the output of this function applied to two different lists of matrices, showing the output side-by-side.
I thought this was just a simple application of matplotlib subplots, as discussed here: https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html
However, I haven't figured out how to get the output of my custom function make_plot to fit into the subplots that I would like.
For example, the provided code on the matplotlib site for how to make horizontal plots is
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Horizontally stacked subplots')
ax1.plot(x, y)
ax2.plot(x, -y)
But if I try a gentle modification like
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Horizontally stacked subplots')
ax1 = make_plot(mats1)
ax2 = mate_plot(mats2)
I get two empty plots, and then the usual outputs of make_plot stacked on top of each other.
I feel like there is something I'm not really understanding about the nature of matplotlib objects, this seems like it should be a simple / reasonable thing to do, but I haven't figured out how to do it yet. Any pointers would be so appreciated!
I'm new in both python and stackoverflow... I come from the ggplot2 R background and I am still getting stacked with python. I don't understand why I have a null plot before my figure using matplotlib... I just have a basic pandas series and I want to plot some of the rows in a subplot, and some on the others (however my display is terrible and I don't know why/how to fix it). Thank you in advance!
df = organism_df.T
fig, (ax1,ax2) = plt.subplots(nrows=1,ncols=2,figsize=(5,5))
ax1 = df.iloc[[0,2,3,-1]].plot(kind='bar')
ax1.get_legend().remove()
ax1.set_title('Number of phages/bacteria interacting vs actual DB')
ax2 = df.iloc[[1,4,5,6,7]].plot(kind='bar')
ax2.get_legend().remove()
ax2.set_title('Number of different taxonomies with interactions')
plt.tight_layout()
The method plot from pandas would need the axes given as an argument, e.g., df.plot(ax=ax1, kind='bar'). In your example, first the figure (consisting of ax1 and ax2) is created, then another figure is created by the plot function (at the same time overwriting the original ax1 object) etc.
I'm trying to display two images side by side in scale. This is the code:
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 15))
ax1.imshow(bird_rescaled)
ax2.imshow(bird_resized)
Unfortunately I just managed to come to this result:
.
What I'd like to have is two in-scale images side by side, kind of like this:
Is there some function argument I'm missing that could solve this?
As mentioned by #mozway, sharey=True helped solving this problem.
I modified the code in this way:
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True, sharex=True, figsize=(12, 6))
ax1.imshow(bird_rescaled)
ax2.imshow(bird_resized)
adding sharex=True
so that now the result looks perfect. [I can't post images because I don't have at least 10 reputation :(]
I also changed the values passed to the figsize argument:
I wanted the images to be 6x6 so the total figure needed to be 12x6.
I am trying to plot a very basic plot putting several parameters together. This is how far I have come. Unfortunately the documentation and its examples does not cover my issue:
fig=plt.figure(figsize=(50,18), dpi=60)
dax_timeseries_xts.plot(color="blue", linewidth=1.0, linestyle="-", label='DAX')
# dax_timeseries_xts is a XTS with dates as index
ax.xaxis.set_minor_locator(dates.WeekdayLocator(byweekday=(1),interval=1))
ax.xaxis.set_minor_formatter(dates.DateFormatter('%d\n%a'))
ax.xaxis.grid(True, which="minor")
ax.yaxis.grid()
ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('\n\n\n%b\n%Y'))
plt.tight_layout()
plt.show()
Where do I create the "ax" in order to make this work?
Or maybe I am not efficiently putting the arguments listed above together to create my chart?
fig, ax_f = plt.subplots(nrows=1, ncols=1)
will give you the axes