How to display many plots together in the same output -python - python

[shot from Jupyter][1]
I am trying to display two plots output from function to be together in the same output.
Can anyone help?
thanks
[1]: https://i.stack.imgur.com/ABXMj.png

You can either use plt.subplots() or else you can do the one as shown in the picture too by coding all the plots together and then giving plt.show() at last.

Below each plot code use plt.plot():
fig, axes = plt.subplots(1,2)
plt.plot(ax=axes[0])
plt.plot(ax=axes[1])

Related

How to loop over all subplot in a figure to add a new series in it?

Doing the following allow me to create a figure with subplots :
DF.plot(subplots=True, layout=(9,3),figsize = (20,40),sharex=False)
plt.tight_layout()
plt.show()
This works very well. However, I would like to add to all these subplots a new series that would be the same for each subplot.
I think it is possible by browsing all subplot in the figure and then edit each one.
I don't know how to do it and don't know the smartest way to do it, maybe avoid using loop would be faster but I don't know if it is doable.
Assuming that the common serie is DF2['Datas'], how do I add it to all subplots?
DataFrame.plot returns a matplotlib.axes.Axes or numpy.ndarray of them.
axs = DF.plot(subplots=True, layout=(9,3),figsize = (20,40),sharex=False)
for ax in axs.ravel():
DF2['Datas'].plot(ax=ax)

Python using custom color in plot

I'm having a problem that (I think) should have a fairly simple solution. I'm still a relative novice in Python, so apologies if I'm doing something obviously wrong. I'm just trying to create a simple plot with multiple lines, where each line is colored by its own specific, user-defined color. When I run the following code as a test for one of the colors it ends up giving me a blank plot. What am I missing here? Thank you very much!
import numpy as np
import matplotlib.pyplot as plt
from colour import Color
dbz53 = Color('#DD3044')
*a bunch of arrays of data, two of which are called x and mpt1*
fig, ax = plt.subplots()
ax.plot(x, mpt1, color='dbz53', label='53 dBz')
ax.set_yscale('log')
ax.set_xlabel('Diameter (mm)')
ax.set_ylabel('$N(D) (m^-4)$')
ax.set_title('N(D) vs. D')
#ax.legend(loc='upper right')
plt.show()
The statement
ax.plot(x, mpt1, color='dbz53', label='53 dBz')
is wrong with 'dbz53' where python treated it as a string of unknown rgb value.
You can simply put
color='#DD3044'
and it will work.
Or you can try
color=dbz53.get_hex()
without quote if you want to use the colour module you imported.
In the plot command, you could enter Hex colours. A much more simple way to beautify your plot would be to simply use matplotlib styles. For instance, before any plot function, just write
plt.style.use('ggplot')

For loop in matplotlib only plots first iteration

I want to read several columns from a csv file and plot them on a single window. What I currently have is this:
fig1=pl.figure(num=1, figsize=(8, 4), dpi=80, facecolor='w',edgecolor='k')
a=np.random.rand(50,9)
ax = pl.gca()
for i in range(0,6,2):
ax.errorbar(a[:,i], a[:,i+1], yerr= a[:,i+2])
ax.set_xscale('log')
ax.set_xlim(1e19, 1e22)
pl.show()
I get no error message, but the output is a plot with only the first iteration, when there should be three in this sample file. I have put different parts of the code in the loop but at best I get the first plot window and two empty windows which is not what I am looking for either. Why isn't the for loop cycling through the i values? Or assuming is it cycling through why is it only plotting the first one? Thanks!
UPDATE: thanks tcaswell, the x range was simply too restrictive. Classical case of not looking at the data close enough. Embarrassing.

multi chart in python

I have the following code:
pl.bar (x1,x2)
pl.show()
pl.plot(x1,x3)
pl.show
It generated two separate chart one bar chart and one plot. I want to have bar and plot in one single graph. Could you please let me know how I can make it?
Thanks,
Amir
Assuming pl is matplotlib.pylab:
pl.bar(x1,x2)
pl.plot(x1,x3,color='r')
pl.show()
Note the change of color for contrast.
Take out the first call to pl.show(). This way you add both items to pl before showing it.

Make more than one chart in same IPython Notebook cell

I have started my IPython Notebook with
ipython notebook --pylab inline
This is my code in one cell
df['korisnika'].plot()
df['osiguranika'].plot()
This is working fine, it will draw two lines, but on the same chart.
I would like to draw each line on a separate chart.
And it would be great if the charts would be next to each other, not one after the other.
I know that I can put the second line in the next cell, and then I would get two charts. But I would like the charts close to each other, because they represent the same logical unit.
You can also call the show() function after each plot.
e.g
plt.plot(a)
plt.show()
plt.plot(b)
plt.show()
Make the multiple axes first and pass them to the Pandas plot function, like:
fig, axs = plt.subplots(1,2)
df['korisnika'].plot(ax=axs[0])
df['osiguranika'].plot(ax=axs[1])
It still gives you 1 figure, but with two different plots next to each other.
Something like this:
import matplotlib.pyplot as plt
... code for plot 1 ...
plt.show()
... code for plot 2...
plt.show()
Note that this will also work if you are using the seaborn package for plotting:
import matplotlib.pyplot as plt
import seaborn as sns
sns.barplot(... code for plot 1 ...) # plot 1
plt.show()
sns.barplot(... code for plot 2 ...) # plot 2
plt.show()
Another way, for variety. Although this is somewhat less flexible than the others. Unfortunately, the graphs appear one above the other, rather than side-by-side, which you did request in your original question. But it is very concise.
df.plot(subplots=True)
If the dataframe has more than the two series, and you only want to plot those two, you'll need to replace df with df[['korisnika','osiguranika']].
I don't know if this is new functionality, but this will plot on separate figures:
df.plot(y='korisnika')
df.plot(y='osiguranika')
while this will plot on the same figure: (just like the code in the op)
df.plot(y=['korisnika','osiguranika'])
I found this question because I was using the former method and wanted them to plot on the same figure, so your question was actually my answer.

Categories

Resources