2-D Plot with two y-axis and only ONE funktion

1 次查看(过去 30 天)
hello,
what i have:
I have a funktion (temperature-profil) which gives me the temperature related to the depth of the ocean.
y-axis: the depth of the ocean
x-axis : temperature
my database: my ocean is 100 km deep and i know the temperature at almost every km, so the pressure
my problem: I would like to add a second y-axis which shows me the pressure at every km. I tried it with plotyy, but i dont want that second graph, i just want the axis. so i tried just to add an axis, that worked. But i dont know how to give there my database instead of "ylim" , i would like that its related to the km of the ocean
my code:
version 1
daten=xlsread('T_profile.xls','sheet4');
figure;
[ax,h1,h2] = plotyy(daten(1:49,2), daten(1:49,1), daten(1:49,1), daten(1:49,4) ); %Temperaturprofil
title('Rp: 1 Ts: 280');
set(ax(1),'YDir','reverse');
set(ax(2),'YDir','reverse');
xlabel('Temperatur in K');
ylabel('Ozeantiefe in km');
version 2
daten=xlsread('T_profile.xls','sheet4');
figure;
plot(daten(1:49,2), daten(1:49,1)); %Temperaturprofil
title('Rp: 1 Ts: 280');
set(gca,'YDir','reverse');
xlabel('Temperatur in K');
ylabel('Ozeantiefe in km');
set(gca,'Box','off');
axesPosition = get(gca,'Position'); %# Get the current axes position
hNewAxes = axes('Position',axesPosition,... %# Place a new axes on top...
'Color','none',... %# ... with no background color
'YLim',[0 10],... %# ... and a different scale
'YAxisLocation','right',... %# ... located on the right
'XTick',[],... %# ... with no x tick marks
'Box','off'); %# ... and no surrounding box
ylabel(hNewAxes,'scale 2'); %# Add a label to the right y axis
i uploaded a picture how it looks right now (ignore the red line) and i would like to have an y axis on the right with the related pressure (the code for that is:
figure;
plot(daten(1:49,2), daten(1:49,1)); %Temperaturprofil
title('Rp: 2 Ts: 280');
set(gca,'YDir','reverse');
xlabel('Temperatur in K');
ylabel('Ozeantiefe in km');
hold on;
plot(daten(1:49,3), daten(1:49,1),'r');%Schmelztemperatur
set(gca,'YDir','reverse');
legend({'Temperaturprofil','Schmelztemperatur'},'Location','southwest')
)
thank you for your help!!

回答(1 个)

Christiaan
Christiaan 2015-3-10
编辑:Christiaan 2015-3-10
Hello,
If I understand you correctly, you would like to plot one figure where at the y axis is the depth, and at the x axis the temperature and the pressure. (also with two axes)
In this example I have made a code for you, that illustrates how this can be achieved.
clc;clear all;close all;
depth = linspace(-120000,0,120001);
temp = 280-60*depth/120000;
pres = 1-depth/10;
x1 = temp;
y1 = depth;
x2 = pres;
y2 = depth;
figure
line(x1,y1/1000,'Color','r');
xlabel('temperature [K]');
ylabel('depth [km]');
legend('temperature [K]','Location','northeast')
set(gca,'xgrid','on','ygrid','on')
ax1 = gca; % current axes
ax1.XColor = 'r';
ax1.YColor = 'k';
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','left',...
'Color','none','XColor','b');
line(x2,y2/1000,'Parent',ax2,'Color','b')
xlabel('Pressure [Bar]');
ylabel('depth [km]');
set(gca,'xgrid','on','ygrid','on')
legend('pressure (Bar)','Location','northwest')
Good Luck! Christiaan
  2 个评论
Mademoiselle_Opossum
Hello Christian!
Thank you very much that you took the time! But I think you understood it wrong - maybe I havent write it clear enouth!
I would like the graph like it is in my attached file, but with the correspondet pressure on the right hand side.
There wouldnt be a second funktion or something - I would like a plot, that when i look at it I see : "oh ok, at 50 km the tempereture is at 295 K and what pressure i have at 50 km?" (that would be on the right hand side - where is nothing right now) is that possible? (so there are to diffrent y-ranges but the second (pressure) would do nothing - just give me the information what pressure i have at a possible depth (the depth is right now on the left hend side y-axis)
Christiaan
Christiaan 2015-3-15
Hello !
You can use the MATLAB function plotyy to plot one x variable (i.e. temperature) vs. two y variables (i.e. depth & pressure). A small example is shown below:
clc;clear all; close all;
depth = linspace(-120000,0,120001);
temp = 280-60*depth/120000;
pres = 1-depth/10;
%temp_depth_pressure = [depth;temp;pres];
[hAx,hLine1,hLine2] = plotyy(temp,depth/1000,temp,pres);grid on;
title('temperature vs. depth & pressure')
xlabel('temperature (K)')
ylabel(hAx(1),'Depth (km)') % left y-axis
ylabel(hAx(2),'Pressure (bar)') % right y-axis
Good Luck! Christiaan

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by