How to insert text outside a plot - text

I have the following plot (with 3 rows and 4 columns):
f, ((ax1, ax2, ax3, ax4), (ax5, ax6, ax7, ax8), (ax9, ax10, ax11, ax12)) = plt.subplots(3, 4, sharex = 'col', sharey = 'row')
ax1.set_title('column1')
ax1.plot([x], [y])
ax5.plot([x1],[y1])
ax9.plot([x2],[y2])
ax2.set_title('column2')
ax2.plot([x3],[x4])
ax6.plot([x5],[x6])
.....
How is it possible to insert text outside the plot window? For each row I would like to write a sentence on the right end side of the plot.

You could use the ax.set_title() function.
Alternatively, you might want to place your text using the ax.text() function. Note that you can use values in the position argument that go beyond your axes ranges. Additionally, you can set the reference coordinate system using the transform parameter.

Related

Is it possible to extract the default tick locations from the primary axis and pass it to a secondary access with matplotlib?

When making a plot with with
fig, ax = plt.subplots()
x=[1,2,3,4,5,6,7,8,9,10]
y=[1,2,3,4,5,6,7,8,9,10]
ax.plot(x,y)
plt.show()
matplotlib will determine the tick spacing/location and value of the tick. Is there are way to extract this automatic spacing/location AND the value? I want to do this so i can pass it to
set_xticks()
for my secondary axis (using twiny()) then use set_ticklabels() with a custom label. I realise I could use secondary axes giving both a forward and inverse function however providing an inverse function is not feasible for the goal of my code.
So in the image below, the ticks are only showing at 2,4,6,8,10 rather than all the values of x and I want to somehow extract these values and position so I can pass to set_xticks() and then change the tick labels (on a second x axis created with twiny).
UPDATE
When using the fix suggested it works well for the x axis. However, it does not work well for the y-axis. For the y-axis it seems to take the dataset values for the y ticks only. My code is:
ax4 = ax.twinx()
ax4.yaxis.set_ticks_position('left')
ax4.yaxis.set_label_position('left')
ax4.spines["left"].set_position(("axes", -0.10))
ax4.set_ylabel(self.y_2ndary_label, fontweight = 'bold')
Y = ax.get_yticks()
ax4.yaxis.set_ticks(Y)
ax4.yaxis.set_ticklabels( Y*Y )
ax4.set_ylim(ax.get_ylim())
fig.set_size_inches(8, 8)
plt.show()
but this gives me the following plot. The plot after is the original Y axis. This is not the case when I do this on the x-axis. Any ideas?
# From "get_xticks" Doc: The locations are not clipped to the current axis limits
# and hence may contain locations that are not visible in the output.
current_x_ticks = ax.get_xticks()
current_x_limits = ax.get_xlim()
ax.set_yticks(current_x_ticks) # Use this before "set_ylim"
ax.set_ylim(current_x_limits)
plt.show()

Bar format (kind) is not displaying the right plot (matplotlib)

I need to do a plot using three variables. One of them should be on the secondary Y axis in bar format (kind), the remaining variables (two) should be on the left axis using a simple line. However, I got the following chart:
When I use the three variables in line format I get the right plot (which is not very useful for a visual analysis):
I did a quick test using a small sample from my data (code below). I get the right pic when I use bar format for the third one.
I wonder, what is going on? Is there a problem with the data size (which I dont think so bcs I get less than 100 rows)?
df2 = pd.DataFrame({'ind':[120.29, 125.45, 127.37, 130.39, 128.30],
'var1':[129.907990, 129.571185, 129.234380, 128.897574, 128.560769],
'var2':[-0.074037, -0.031806, -0.014426, 0.011578, -0.002028]})
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
df2['ind'].plot(ax=ax1)
df2['var1'].plot(ax=ax1)
df2['var2'].plot(kind='bar', ax=ax2, color='r')
plt.show()
PD: In addition, I noted that in the third pic the line is behind the bar. How can I change that?
I found the solution for this (this link helped me a lot ). Basically, it is based on the index you set up previously.
This is the new code:
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(df2.index, df2['ind'])
ax1.plot(df2.index, df2['var1'])
ax2.bar(df2.index, df2['var2'], color='r')
plt.show()
Hope this helps.

Plot the distance between every two points in 2 D

If I have a table with three columns where the first column represents the name of each point, the second column represent numerical data (mean) and the last column represent (second column + fixed number). The following an example how is the data looks like:
I want to plot this table so I have the following figure
If it is possible how I can plot it using either Microsoft Excel or python or R (Bokeh).
Alright, I only know how to do it in ggplot2, I will answer regarding R here.
These method only works if the data-frame is in the format you provided above.
I rename your column to Name.of.Method, Mean, Mean.2.2
Preparation
Loading csv data into R
df <- read.csv('yourdata.csv', sep = ',')
Change column name (Do this if you don't want to change the code below or else you will need to go through each parameter to match your column names.
names(df) <- c("Name.of.Method", "Mean", "Mean.2.2")
Method 1 - Using geom_segment()
ggplot() +
geom_segment(data=df,aes(x = Mean,
y = Name.of.Method,
xend = Mean.2.2,
yend = Name.of.Method))
So as you can see, geom_segment allows us to specify the end position of the line (Hence, xend and yend)
However, it does not look similar to the image you have above.
The line shape seems to represent error bar. Therefore, ggplot provides us with an error bar function.
Method 2 - Using geom_errorbarh()
ggplot(df, aes(y = Name.of.Method, x = Mean)) +
geom_errorbarh(aes(xmin = Mean, xmax = Mean.2.2), linetype = 1, height = .2)
Usually we don't use this method just to draw a line. However, its functionality fits your requirement. You can see that we use xmin and ymin to specify the head and the tail of the line.
The height input is to adjust the height of the bar at the end of the line in both ends.
I would use hbar for this:
from bokeh.io import show, output_file
from bokeh.plotting import figure
output_file("intervals.html")
names = ["SMB", "DB", "SB", "TB"]
p = figure(y_range=names, plot_height=350)
p.hbar(y=names, left=[4,3,2,1], right=[6.2, 5.2, 4.2, 3.2], height=0.3)
show(p)
However Whisker would also be an option if you really want whiskers instead of interval bars.

How to make subplots in python?

I am trying to make subplots.
I call many columns from a dataframe, turn them into array and plot them.
I want to plot them in 4 rows, 2 columns. But I only get 1 column (you can check the image). What am I doing wrong?
Here is my code:
for column in df3: #I call the dataframe
data=df3[column].values #Turn it into an array
fig = plt.figure()
plt.subplot (4,2,1) #I want 4 rows and 2 columns
ax,_=plot_topomap(data, sensors_pos, cmap='viridis', vmin=0, vmax=100, show=False)
plt.title("KNN" + " " + column) #This is the title for each subplot
fig.colorbar(ax)
plt.show
There are several things which might cause problems in your code and it's hard to find a solution without knowing the complete code.
In your code you create several figures. However, you really want one single figure. So the figure needs to be creates outside the loop.
Then you want to create subplots, so in every loop step you need to tell matplotlib to which subplot it should plot. This can be done by ax = fig.add_subplot(4,2,n) where n is a number which you increase in every run of the loop.
Next you call plot_topomap. But how would plot_topomap know what to plot where? You need to tell it, by supplying the keyword argument axes = ax.
Finally try to set a colorbar, with the return image as argument to the axes ax.
Of course I cannot test the following code, but it might do what you want, in case I interpreted everything well.
n = 1
fig = plt.figure()
for column in df3: #I call the dataframe
data=df3[column].values #Turn it into an array
ax = fig.add_subplot(4,2,n) #I want 4 rows and 2 columns
im,_ = plot_topomap(data, sensors_pos, cmap='viridis', vmin=0, vmax=100, show=False, axes=ax)
ax.set_title("KNN" + " " + column) #This is the title for each subplot
fig.colorbar(im, ax=ax)
n+=1

How to use string as data for plotting in Matlab?

I want to use a words like, let's say, 'A', 'B' and 'C' on X-axis to show their corresponding properties on Y-axis. How can I write these strings on X-axis instead of numerical data?
Use 'XTick' and 'XTickLabel' properties of the axes handle.
Here's a simple example:
x = 1:5;
y = rand(size(x));
plot(x, y, 'b')
set(gca, 'XTick',1:5, 'XTickLabel',{'A' 'B' 'C' 'D' 'E'})
Set yourself up a cell with your letters (mine's called labels), then use the XTick property to set the same amount of ticks on the x axis as your label number. Finally, the XTickLabel property will write your labels to the x axis.
x = yourXdata;
y = yourYdata;
labels = {'A' 'B' 'C'};
plot(x, y);
set(gca, 'XTick', 1:3, 'XTickLabel', labels);
How to use CHARACTER Values instead of Numerical values in X axis.
to label x as T1 T2 T3 T4 just use this :
set(gca,'XTick',1:4,'XTickLabel',{'T1', 'T2', 'T3', 'T4'},'FontSize',15)
this command can be used after the plot command followed by the xlabel and ylabel , legand commands.
you can also adjust the font size.
Practical Example:
%% 50% Day
T1wSI=[54.17 115];
T2wSI=[53.5 112];
T3wSI=[52.2 110];
T4wSI=[51.2 108];
T1oSI=[50.25 94];
T2oSI=[49.18 92];
T3oSI=[48.2 90];
T4oSI=[46.1 84];
table1=[T1wSI;T2wSI;T3wSI;T4wSI;T1oSI;T2oSI;T3oSI;T4oSI ];
season2012=table1(:,1);
season2013=table1(:,2);
Tr1=[1 2 3 4];
Treatment1 =['T1wSI' 'T2wSI' 'T3wSI' 'T4wSI' 'T1oSI' 'T2oSI' 'T3oSI' 'T4oSI'];
%Tre1=['T1' 'T2' 'T3' 'T4'];
%set(gca,'FontSize',14)
figure(1)
set(gca,'XTick',1:4,'XTickLabel',{'T1', 'T2', 'T3', 'T4'},'FontSize',14)
plot(Tr1,table1(1:4,1),'--bs','LineWidth',3);% 2012
hold on;
plot(Tr1,table1(1:4,2),'-go','LineWidth',3);% 2013
plot(Tr1,table1(5:8,1),'--r*','LineWidth',3); % 2012
plot(Tr1,table1(5:8,2),'-m^','LineWidth',3);% 2013
set(gca,'XTick',1:4,'XTickLabel',{'T1', 'T2', 'T3', 'T4'},'FontSize',15)
xlim=[1 5];
xlabel('Treatments')
ylabel('Days to 50 % Flowering')
legend('With -Season 2012','Without -Season 2013','With -Season 2012','Without - Season 2013','Location','NorthEast');
You can also do this using the GUI.
1) Click on the figure axes to to open the Axes Property Editor.
2) Click on the "More properties" button on the right side of the window. This will open the inspector window of the axes.
3) Click on the small button next to "XTickLabel" property to open the dialogue box as shown below.
4) Enter the labels you want and click on "OK".

Resources