Western movies and television shows are set in the wild west of imagination, which is more or less based on the real western half or two thirds of the USA usually between about 1850 and 1900.
So westerns are based on a real time and a real vast region, and thus might be supposed to happen in that real time and real region. But many westerns change so many aspects of the real west that they might as well be fantasies set in Middle-earth, or Narnia, or be science fiction stories set on other planets centuries in the future.
For example, you may have heard of John Ford's "cavalry trilogy", three very, very, loosely connected films.
in Fort Apache (1948) the garrison at Fort Apache get a message from Fort Grant that Diablo's band has left the reservation and is headed for Mexico. So they intercept and capture Diablo's band. So Fort Apache should be south of the reservation on the direct route to Mexico, and Fort Grant should also be close to the reservation and thus north of Fort Apache. But the real Fort Apache was north of the real Fort Grant.
Later Captain York goes to Mexico to negotiate with Cochise, and crosses a deep river canyon at the border. But the border of Arizona with Mexico is two straight lines through the desert, with no large canyons along it.
In Rio Grande (1950) the border with Mexico is the shallow river Rio Grande or Rio Bravo. The Rio Grande is the border between the USA and Mexico in Texas and nowhere else. But characters come from Texas to Fort Starke, which thus can't be in Texas. Thus the fictional Fort Starke in Rio Grande (1950) would have to be in the eastern section of New Mexico, north of the far western part of Texas. And Fort Starke would have to be pretty far south in New Mexico to be only a few days ride from the border with Mexico.
But in She Wore a Yellow Ribbon (1949) the fictional Fort Stark is somewhere on the southern plains, possibly in or near The Indian Territory (now the state of Oklahoma) and hundreds of miles away from it's location in Rio Grande (1950).
And this is just a tiny sample of the way western movies move towns, forts, railroads, rivers, mountains, Indian tribes, etc. around with no regard for real geography.
And of course western movies and tv shows often change many other aspects of the west beside the geography.
And that goes for many other genres. Most genres of films, television, and fiction are set on the planet Earth, at the time they are written or a small time in the near future, or sometime during the past. And a lot of writers work very hard to get details about their settings correct. And a lot of writers do not.
I certainly hope that many writers will try their best to get every detail correct. But I know that many writers will not. And I know that many writers who do try to get every detail correct will fail because of problems beyond their control.
So unless the setting of your story is some time and place that is totally outside the scope of present day knowledge of the real universe, your story is going to have some factual and correct details, at least a few. And unless you are very good at learning the facts about your setting, and/or very lucky, your story is going to have some inaccurate details, at least a few.
If you want to write a science fiction story or a fantasy story you should probably ask some questions in the World Building stack e Exchange.
Suppose that you want to write a series of fantasy stories and novels set in a fantasy kingdom and you want it to be full of intrigue, back stabbing, etc., etc. very Game of Thrones like, over a period of centuries.
Examples of real dynasties you might want to model your fictional dynasty on include The Ptolemaic dynasty of Egypt (305-30 BC) in the later generations, the Merovingian dynasty of Gaul from about AD 450 to 753, and the Norwegian dynasty during the civil wars period from 1130 to 1240, for example.
If you use one or several of them as models for your dynastic history in a period of intrigue, civil war, and back stabbing, you would want to change the geography and previous history of your fictional country a little bit, and create a partial fictional language language for that country for the purpose of creating personal and place names which may be based upon or modified from names in the language of your examples.
And the same goes for any other type of story you might want to write. You can get partial inspiration from real persons, places, things, and events and make your fictional versions more or less similar to the ones which inspired your story. And in my opinion there is a very wide range in how much more or less similar your fiction can be to the real events which inspired it.