Group Abstract Group Abstract

Message Boards Message Boards

0
|
28 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Changing units in a DateListPlot

Posted 14 hours ago

I asked Notebook Assistant to make a nice plot of the United States population from 2014 to 2023 with population in millions. I'd appreciate help with coding to accomplish this. Notebook Assistant generated code that does not work:

usPopulationData=CountryData["UnitedStates",{"Population",{2014,2023}}];
usPopulationDataM=usPopulationData /. Quantity[v_,"People"]:>Quantity[v/1000000,"People"];
DateListPlot[
    usPopulationDataM,
    Joined->True,
    PlotTheme->"Scientific",
    Filling->Axis,
    PlotLabel->Style["U.S. Population from 2014 to 2023",Bold,FontColor->DarkBlue,FontSize->16],
    Frame->True,
    FrameStyle->Directive[FontSize->12,Bold],        FrameLabel->{Style["Year",Bold,DarkBlue,12],Style["Population (Millions)",Bold,DarkBlue,12]},
    GridLines->Automatic,
    GridLinesStyle->Directive[Dashed,Gray],
    PlotStyle->Directive[Thick,Red],
    ImageSize->Large
]

The biggest problem is that the calculation of usPopulationDataM doesn't work. In fact, usPopulationDataM == usPopulationData returns True. For details about the code and its problems, see attached file NotebookAssitantIssuesGDorfman11jul25.nb .

Attachments:
POSTED BY: Gerald Dorfman
2 Replies

Gianluca, Thanks. That worked. There was one other, minor problem, which I fixed by changing DarkBlue to Blue. I also changed ImageSize to Medium. The result is in attached file NotebookAssitantIssuesGDorfman12jul25.nb , which is the same as the file I provided yesterday but has a new Subsection called "Modified Code".

Attachments:
POSTED BY: Gerald Dorfman

If you try usPopulationData // InputForm you will see that Quantity does not appear inside, but rather QuantityArray. You can try this:

UnitConvert[usPopulationData, "Mega"*"People"]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard