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.
Related
I need to do what has been explained for MATLAB here:
How to show legend for only a specific subset of curves in the plotting?
But using Python instead of MATLAB.
Brief summary of my goal: when plotting for example three curves in the following way
from matplotlib import pyplot as plt
a=[1,2,3]
b=[4,5,6]
c=[7,8,9]
# these are the curves
plt.plot(a)
plt.plot(b)
plt.plot(c)
plt.legend(['a','nothing','c'])
plt.show()
Instead of the word "nothing", I would like not to have anything there.
Using '_' will suppress the legend for a particular entry as following (continue reading for handling underscore _ as a legend). This solution is motivated by the recent post of #ImportanceOfBeingEarnest here.
plt.legend(['a','_','c'])
I would also avoid the way you are putting legends right now because in this way, you have to make sure that the plot commands are in the same order as legend. Rather, put the label in the respective plot commands to avoid errors.
That being said, the straightforward and easiest solution (in my opinion) is to do the following
plt.plot(a, label='a')
plt.plot(b)
plt.plot(c, label='c')
plt.legend()
As #Lucas pointed out in comment, if you want to show an underscore _ as the label for plot b, how would you do it. You can do it using
plt.legend(['a','$\_$','c'])
I'm having trouble increasing the size of my plot figures using Seaborn (imported as sns). I'm using sns.pairplot to plot columns of a data frame against one another.
%matplotlib inline
plt.rcParams['figure.figsize']=10,10
columns=list(df.columns.values)
g=sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])
The plots populate with data just fine, but the figure size is too small.
I thought plot.rCParams['figure.figsize'] would control how large the figure is, but it doesn't seem to take effect. I've tried a few different suggestions from online boards, but nothing seems to work.
sns.pairplot
"Returns the underlying PairGrid instance for further tweaking"
...for instance changing the figure size:
g=sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])
g.fig.set_size_inches(15,15)
Try to put the size in parenthesis, this does the trick for me:
plt.rcParams['figure.figsize']=(10,10)
In addition to the well working answer by #MartinAnderson, seaborn itself provides the option to set the height of the subplots of the grid. In combination with the aspect this determines the overall size of the figure in dependence of the number of subplots in the grid.
In seaborn <= 0.8.1:
g = sns.pairplot(..., size=10, aspect=0.6)
In seaborn >= 0.9.0:
g = sns.pairplot(..., height=10, aspect=0.6)
Note that this applies to all seaborn functions which generate a figure level grid, like
pairplot, relplot, catplot, lmplot and the underlying PairGrid or FacetGrid.
For other seaborn plots, which directly plot to axes, the solutions from How do you change the size of figures drawn with matplotlib? will work fine.
If we would like to change only the height or only the width then you can do
g = sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])
g.fig.set_figheight(6)
g.fig.set_figwidth(10)
You can use set for style control : https://seaborn.pydata.org/generated/seaborn.set.html
sns.set(rc={'figure.figsize':(20,10)})
Reffering to Rahul's question about sns.catplot ( Unable to change the plot size with matplotlib and seaborn )
If you try in jupyter notebook:
plt.figure(figsize=(25,20))
sns.boxplot(x='CriticRating', y='AudienceRating', data=movies)
it is working, but
sns.boxplot(x='CriticRating', y='AudienceRating', data=movies)
plt.figure(figsize=(25,20))
is not working (plot is very small). It's important to add line plt.figure(figsize=(25,20)) before sns.boxplot()and include %matplotlib inline of course in order to display plot in jupyter.
You can use set for style control : https://seaborn.pydata.org/generated/seaborn.set.html
We can accomplish this using sns.set() For example:
sns.set(rc={'figure.figsize':(20,10)})
Though, the preferred syntax is:
sns.set_theme()
NB: sns.set() or sns.set_theme() is a high level command that will set the (parameters) for everything else in your notebook.
sns.set(rc={'figure.figsize':(20,10)}) will set this to be the figure size for everything else in your notebook
Disclaimer: I am very inexperienced using matplotlib and python in general.
Here is the figure I'm trying to make:
Using GridSpec works well for laying out the plots, but when I try to include a colorbar on the right of each row, it changes the size of the corresponding subplot. This seems to be a well known and unavoidable problem with GridSpec. So at the advice of this question: Matplotlib 2 Subplots, 1 Colorbar
I've decided to remake the whole plot using ImageGrid. Unfortunately the documentation only lists the options cbar_mode=[None|single|each] whereas I want 1 colobar per row. Is there a way to do this inside a single ImageGrid? or will I have to make 2 grids and deal with the nightmare of alignment.
What about the 5th plot at the bottom? Is there a way to include that in the image grid somehow?
The only way I can see this working is to somehow nest two ImageGrids into a GridSpec in a 1x3 column. this seems overly complicated and difficult so I don't want to build that script until I know its the right way to go.
Thanks for any help/advice!
Ok I figured it out. It seems ImageGrid uses subplot somehow inside it. So I was able to generate the following plot using something like
TopGrid = ImageGrid( fig, 311,
nrows_ncols=(1,2),
axes_pad=0,
share_all=True,
cbar_location="right",
cbar_mode="single",
cbar_size="3%",
cbar_pad=0.0,
cbar_set_cax=True
)
<Plotting commands for the top row of plots and colorbar>
BotGrid = ImageGrid( fig, 312,
nrows_ncols=(1,2),
axes_pad=0,
share_all=True,
cbar_location="right",
cbar_mode="single",
cbar_size="3%",
cbar_pad=0.0,
)
<Plotting commands for bottom row . . .>
StemPlot = plt.subplot(313)
<plotting commands for bottom stem plot>
EDIT: the whitespace in the color plots is intentional, not some artifact from adding the colorbars
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')
I'm having trouble increasing the size of my plot figures using Seaborn (imported as sns). I'm using sns.pairplot to plot columns of a data frame against one another.
%matplotlib inline
plt.rcParams['figure.figsize']=10,10
columns=list(df.columns.values)
g=sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])
The plots populate with data just fine, but the figure size is too small.
I thought plot.rCParams['figure.figsize'] would control how large the figure is, but it doesn't seem to take effect. I've tried a few different suggestions from online boards, but nothing seems to work.
sns.pairplot
"Returns the underlying PairGrid instance for further tweaking"
...for instance changing the figure size:
g=sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])
g.fig.set_size_inches(15,15)
Try to put the size in parenthesis, this does the trick for me:
plt.rcParams['figure.figsize']=(10,10)
In addition to the well working answer by #MartinAnderson, seaborn itself provides the option to set the height of the subplots of the grid. In combination with the aspect this determines the overall size of the figure in dependence of the number of subplots in the grid.
In seaborn <= 0.8.1:
g = sns.pairplot(..., size=10, aspect=0.6)
In seaborn >= 0.9.0:
g = sns.pairplot(..., height=10, aspect=0.6)
Note that this applies to all seaborn functions which generate a figure level grid, like
pairplot, relplot, catplot, lmplot and the underlying PairGrid or FacetGrid.
For other seaborn plots, which directly plot to axes, the solutions from How do you change the size of figures drawn with matplotlib? will work fine.
If we would like to change only the height or only the width then you can do
g = sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])
g.fig.set_figheight(6)
g.fig.set_figwidth(10)
You can use set for style control : https://seaborn.pydata.org/generated/seaborn.set.html
sns.set(rc={'figure.figsize':(20,10)})
Reffering to Rahul's question about sns.catplot ( Unable to change the plot size with matplotlib and seaborn )
If you try in jupyter notebook:
plt.figure(figsize=(25,20))
sns.boxplot(x='CriticRating', y='AudienceRating', data=movies)
it is working, but
sns.boxplot(x='CriticRating', y='AudienceRating', data=movies)
plt.figure(figsize=(25,20))
is not working (plot is very small). It's important to add line plt.figure(figsize=(25,20)) before sns.boxplot()and include %matplotlib inline of course in order to display plot in jupyter.
You can use set for style control : https://seaborn.pydata.org/generated/seaborn.set.html
We can accomplish this using sns.set() For example:
sns.set(rc={'figure.figsize':(20,10)})
Though, the preferred syntax is:
sns.set_theme()
NB: sns.set() or sns.set_theme() is a high level command that will set the (parameters) for everything else in your notebook.
sns.set(rc={'figure.figsize':(20,10)}) will set this to be the figure size for everything else in your notebook