# How to plot 2 columns (one is population the other is years) in a linear graph

84 vues (au cours des 30 derniers jours)
Dionysios le 4 Fév 2023
Commenté : Star Strider le 4 Fév 2023
I have an excel file with 2 columns. I want to plot the data in a simple linear graph. The problem lies with the years. I keep getting the error message:"Error using plot. When table is the second input, the first input must be a valid parent."
My code is as follows:
% figure_1
figure
plot(years,Population,'kx-')
xlabel ('Year','FontSize',16,'FontWeight','bold','Color','b')
ylabel ('Population (Billions)','FontSize',16,'FontWeight','bold','Color','b')
title ('Linear Growth','FontSize',20,'Color')
##### 4 commentairesAfficher 2 commentaires plus anciensMasquer 2 commentaires plus anciens
Dyuman Joshi le 4 Fév 2023
It is tought to tell from such limited code. Can you post or upload your code?
And please copy-paste the full error message.
Voss le 4 Fév 2023
That's because you say 'Color' but you don't supply a color
title ('Linear Growth','FontSize',20,'Color')
% ^ missing color
e.g.,
title ('Linear Growth','FontSize',20,'Color','g')

Connectez-vous pour commenter.

### Réponses (1)

Star Strider le 4 Fév 2023
This is difficult without the Excel file.
Taking a wild guess:
plot(Population.years, Population.Population)
alternatively:
plot(Population{:,1}, Population{:,2})
The second is more likely to be successful.
Note the curly brackets {} to do the table addressing.
.
##### 5 commentairesAfficher 3 commentaires plus anciensMasquer 3 commentaires plus anciens
Voss le 4 Fév 2023
Star Strider le 4 Fév 2023
Sure!
Lv = T1{:,1} <= 2015; % Years Up To & Including 2015
figure
plot(T1{Lv,1}, T1{Lv,2}, 'kx-')
grid
xlabel ('Year','FontSize',16,'FontWeight','bold','Color','b')
ylabel ('Population (Billions)','FontSize',16,'FontWeight','bold','Color','b')
title ('Linear Growth','FontSize',20,'Color','g')
.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Graphics dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by