how to bold only one axis?

253 visualizzazioni (ultimi 30 giorni)
Minka Califf
Minka Califf il 10 Giu 2018
Commentato: Viswajit Talluru il 16 Ott 2023
I am trying to make the blue y-axis on the right bold like the lines it is referencing, and for the life of me cannot figure out how without bolding the whole box. any help would be appreciated. my apologies for the long code:
clear all;clc;close all;
%enter data
CA1=[138986 123678 125128 118552 115738 134278];
FL1=[48069 55599 56687 47862 35900 32190];
NY1=[62601 61067 63445 77430 88250 89503];
CA2=[0.651 0.589 0.595 0.616 0.637 0.682];
FL2=[0.573 0.607 0.633 0.589 0.474 0.468];
NY2=[0.085 0.059 0.058 0.054 0.046 0.051];
x=2007:2:2017;
f1=figure(1);
left_color = [0 0 0];
right_color = [0 0 1];
set(f1,'defaultAxesColorOrder',[left_color; right_color]);
yyaxis right
%make axis bold set(gca,'linewidth',2)
plot(x,CA2,'b','LineWidth',2)
hold on
plot(x,NY2,'b--','LineWidth',2)
plot(x,FL2,'b:','LineWidth',2)
ylabel('Percent Unsheltered')
set(get(gca,'YLabel'),'Rotation',-90)
yyaxis left
b=bar(x,transpose([CA1/1000;NY1/1000;FL1/1000]));
b(1).FaceColor = '0.9294 0.6902 0.1294';
b(2).FaceColor = '0.9294 0.6000 0.5020';
b(3).FaceColor = '1.0000 1.0000 0.7020';
ylabel('Homeless Population (Thousands)')
ylim([0 150])
%line([2006.1 2006.1],[0 150],'Color','k','LineWidth',2)
% line([2010 2010],[0 150],'Color','k','LineWidth',2)
annotation('doublearrow',[0.14 0.31],[0.7 0.7],'LineWidth',1)
%gray box
annotation('rectangle',[0.13 0.11 0.193 0.81],'FaceColor',[0.6 0.6 0.6],'EdgeColor', 'none','FaceAlpha',0.2)
text(2007.15,115,'Recession')
%str = {'CA -','NY --','FL ..'};
%annotation('textbox',[0.57 0.55 0.2 0.2],'String',str,'FitBoxToText','on')
%legend
h1=plot([1:10],'Color','b','DisplayName','This one');hold on;
h2=plot([1:2:10],'Color','b','DisplayName','This two');
h3=plot([1:3:10],'Color','b','DisplayName','This three');
legend([h1 h2 h3],{'\color{blue} CA','\color{blue} FL','\color{blue} NY'})
legend boxoff % Hides the legend's axes (legend border and background)
text(2006.42,141,'CA')
text(2006.9,66,'NY')
text(2007.35,51,'FL')
text(2008.42,126,'CA')
text(2008.9,64,'NY')
text(2009.35,59,'FL')
text(2010.42,128,'CA')
text(2010.9,66,'NY')
text(2011.35,59,'FL')
text(2012.42,121,'CA')
text(2012.9,80,'NY')
text(2013.35,51,'FL')
text(2014.42,118,'CA')
text(2014.9,91,'NY')
text(2015.35,39,'FL')
text(2016.42,136.5,'CA')
text(2016.9,92,'NY')
text(2017.35,35,'FL')
xlim([2006 2018])
xlabel('year');
xticks([2007 2009 2011 2013 2015 2017])
%white background
set(gcf,'color','w');
%window size
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 .5 .5]);
%turn on grid lines
set(gca,'XGrid','off')
set(gca,'YGrid','on')
title('Largest Homeless Populations 2007-2017', 'fontsize', 18);

Risposta accettata

Image Analyst
Image Analyst il 10 Giu 2018
Minka, Alfonso's solution only makes the text next to the axis bold. It does not bold the axis itself. To do that you must specify the 'LineWidth' property of the specific axis. See this demo:
plot(1:10);
% Get handle to the axes graphical object.
ax = gca
% Make the x axis only have a font size of 9 and text weight of bold
xlabel('X Axis', 'FontSize', 9, 'FontWeight', 'bold');
% Make the x axis (line) and tick marks have a line width of 2 and color red.
ax.XAxis.LineWidth = 2;
ax.XAxis.Color = 'r';
% Make the x axis only have a font size of 14 and text weight of bold, and color blue.
ylabel('Y Axis', 'FontSize', 14, 'FontWeight', 'bold');
% Make the x axis (line) and tick marks have a line width of 5, and color dark green.
ax.YAxis.LineWidth = 5;
ax.YAxis.Color = 'b';
  2 Commenti
Cg Gc
Cg Gc il 14 Feb 2019
Modificato: Cg Gc il 14 Feb 2019
This works great on making the lines thicker, but does nothing to the text of the axis itself. I mean the numbers beside the axis, not the label.
How does one go about making the text along the axis bold?
I stumbled upon the answer.
h = gca;
h.YAxis.FontWeight = 'bold';
h.XAxis.FontWeight = 'bold';
Viswajit Talluru
Viswajit Talluru il 16 Ott 2023
How do you bold YYaxis is it supposed to work the same way?

Accedi per commentare.

Più risposte (1)

Alfonso
Alfonso il 10 Giu 2018
Look at this example:
% Only put in bold X axis
plot(randn(100,1));
title('X axis in bold');
xlabel('X axis','fontweight','bold');
% Only put in bold Y axis
plot(randn(100,1));
title('Y axis in bold');
ylabel('Y axis','fontweight','bold');

Categorie

Scopri di più su Labels and Annotations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by