What are the parameters of pyplot.subplot? [duplicate] - matplotlib

This question already has answers here:
What does the argument mean in fig.add_subplot(111)?
(8 answers)
What is the behaviour of subplot given single argument? [duplicate]
(1 answer)
Why do many examples use `fig, ax = plt.subplots()` in Matplotlib/pyplot/python
(6 answers)
Closed 1 year ago.
Can't understand pyplot.subplot(330+1+i)
# plot first few images
for i in range(9):
#define subplot
pyplot.subplot(330 + 1 + i)
# plot raw pixel data
pyplot.imshow(trainX[i], cmap=pyplot.get_cmap('gray'))
# show the figure
pyplot.show()

You can view the arguments in the documentation for subplot. The basic arguments are subplot(nrows, ncols, index) where nrows is the number of rows of plots, ncols is the number of columns of plots, and index is the plot number when counting across the grid of plots from left to right, top to bottom.
Another way to specify subplots is with a three-digit integer as in your example.
A 3-digit integer. The digits are interpreted as if given separately as three single-digit integers, i.e. fig.add_subplot(235) is the same as fig.add_subplot(2, 3, 5). Note that this can only be used if there are no more than 9 subplots.
In your example where i ranges from 0 to 8, inclusive, the argument for subplot will range from 331 to 339. Following to the documentation, your subplots will be over 3 rows and 3 columns with in indices 1 to 9.

Related

Plot multiple boxplots from seaborn with hue [duplicate]

This question already has answers here:
Boxplot of Multiple Columns of a Pandas Dataframe on the Same Figure (seaborn)
(4 answers)
Closed 4 months ago.
I have a dataframe with three columns
df=pd.DataFrame(data = {'Dose1': [1,2,3,4,5], 'Dose2': [6,6,4,7,4],'SickOrNot':[True,False,True,True,False]})
The last column corresponds to whether or not a patient stayed sick and the first two columns are doses of two drugs administered to the patient. I want to create two pairs of boxplots (in seaborn) of the doses, using whether the patient was sick or not as a hue.
So, essentially, I want the x axis to have two sections (Dose 1, Dose 2), which each section containing two boxplots. So that my final four boxplots are that of Dose 1 of sick patients, dose 1 of non sick patients, dose 2 of sick patients, dose 2 of non sick patients.
What is the syntax that I would use to do this? I have tried setting hue to be 'sick or not' but I am very confused about what to set as my x and y values when calling sns.boxplot.
Reshape the data into long form such that each column is one variable and each row is one observation. In this case Dose1 and Dose2 should be combined into one column, e.g. Section.
melt() the data with SickOrNot as the identifier and Dose1 and Dose2 as the values. Then set SickOrNot as the plot's hue:
sns.boxplot(
data=df.melt(id_vars=['SickOrNot'], value_vars=['Dose1', 'Dose2'],
var_name='Section', value_name='Dosage'),
x='Section',
y='Dosage',
hue='SickOrNot',
)

increasing x y values in matpplot [duplicate]

This question already has answers here:
How to set ticks on Fixed Position , matplotlib
(2 answers)
Closed 2 years ago.
I have made a plot in Jupyter and I got output as shown in picture but I want to increase my x values like it has 1995,2000,2005,2010,2015 on x axis and I want more x values like say 1995,1997,1999,2001,2003...so on.
I have enter this code but I am unable to produce more x values and y values as mentioned before.
fig=plt.figure(figsize=(9, 7), dpi= 100, facecolor='w', edgecolor='k')
plt.plot(df_3)
You can use xticks such as
plt.xticks(min_x, max_x+1, 1.0)
You can also set your interval 2 or 3 to have more number on your x-axis.

how to select specific rows and columns of a double array in numpy to form a submatrix? [duplicate]

This question already has answers here:
Index a 2D Numpy array with 2 lists of indices
(5 answers)
Is there a way to do array-based indexing in NumPy so that indices corresponding to all possible combinations of indexing array elements are selected? [duplicate]
(1 answer)
Closed 2 years ago.
say I have a numpy list of lists:
import numpy as np
ab=np.array([[1,2,3,4,5],[2,3,4,5,6],[3,4,5,3,6]])
Now say I want to have the submatrix the first two rows and column 1 and 3, I would do
print(ab[0:2,[1,3]])
Now if I want row 0 and 2 and columns 1 and 3, I would try:
print(ab[np.array([0,2]),np.array([1,3])])
But then I get :
[3 6]
This is entries wise seeking and not rows and columns wise submatrixing.
How do you do it?
When you select sub-arrays with two broadcastable indices arrays, like array[arr_1, arr2], it will match each element of arr_1 to arr_2 and select corresponding element of array. If you wish to select all rows in arr_1 and all columns in arr_2, the most elegant way would be using np.ix_. The code would be:
ab[np.ix_(np.array([0,2]),np.array([1,3]))]
output:
[[2 4]
[4 3]]
About np.ix_: From numpy doc: This function takes N 1-D sequences and returns N outputs with N dimensions each, such that the shape is 1 in all but one dimension and the dimension with the non-unit shape value cycles through all N dimensions.
Which means you can extend this to ANY dimension array. For array of N dimensions, calling np.ix_(arr_1, arr_2, ..., arr_N) will create N indices array, each will cycle through all arr_i rows of dimension i in array.

Generate legend with matplotlib based on the values in column [duplicate]

This question already has answers here:
Scatter plots in Pandas/Pyplot: How to plot by category [duplicate]
(8 answers)
Closed 3 years ago.
Assume we have a dataframe of 4 individuals' scores in 2 different tests and the 3rd column tells us if they passed or failed overall
df:
[10,20,failed
10,40,passed
20,40,passed
30,10,failed]
I would like to generate a scatter plot with the scores of the 1st column on the x axis, the scores of the 2nd test on the y axis, and indicate with color (or marker) if they passed or failed. I have achieved this with:
plt.scatter(x=df[column1], y=df[column2], c=df[column3])
The question is, how can I have a legend based on the color (or marker) and column3?
[red: failed
blue: passed]
Here's my suggestion: Plot the failed an passed separately to get their handles, which can then be used for the legend.
fig = plt.figure()
ax1 = fig.add_subplot(111)
passed = ax1.scatter(x=df.loc[df[column3].eq('passed'), column1], y=df.loc[df[column3].eq('passed'), column2], c='green')
failed = ax1.scatter(x=df.loc[df[column3].eq('failed'), column1], y=df.loc[df[column3].eq('failed'), column2], c='red')
ax1.legend(handles=[passed, failed], labels=['Passed', 'Failed'])

How do I plot 2 columns of a pandas dataframe excluding rows selected by a third column [duplicate]

This question already has answers here:
Scatter plots in Pandas/Pyplot: How to plot by category [duplicate]
(8 answers)
Closed 4 years ago.
I have a pandas dataframe that looks like this:
Nstrike Nprem TDays
0 0.920923 0.000123 2
1 0.951621 0.000246 2
2 0.957760 0.001105 2
..............................
16 0.583251 0.000491 7
17 0.613949 0.000614 7
18 0.675344 0.000368 7
..............................
100 1.013016 0.029592 27
101 1.043713 0.049730 27
102 1.074411 0.071218 27
etc.
I would like to plot a graph of col.1 vs col.2, in separate plots as selected by col.3, maybe even in different colors.
The only way I can see to do that is to separate the dataframe into discrete dataframes for each col.3 value.
Or I could give up on pandas and just make the col.3 subsets into plain python arrays.
I am free to change the structure of the dataframe if it would simplify the problem.
IIUC, you can use this as a skeleton, and customize it how you want:
for g, data in df.groupby('TDays'):
plt.plot(data.Nstrike, data.Nprem, label='TDays '+str(g))
plt.legend()
plt.savefig('plot_'+str(g)+'.png')
plt.close()
Your first plot will look like:
Your second:
And so on