Tens of thousands of anti-U.S. demonstrators marched through Madrid and other Spanish cities today on the eve of President Reagan's arrival for a two-day state visit here.

Organizers claimed that 500,000 took part in the Madrid protest, but police put the figure at 75,000.

The rallies were largely festive, although there were scattered incidents of violence. In Barcelona, an American flag was burned and the door of the U.S. Consulate was broken, but no protesters entered the building. In Madrid, stones were thrown at office windows of the conservative Popular Alliance party, Spain's main opposition group. No arrests were reported.

Walls and construction fences in Madrid and other Spanish cities have been plastered with bitter cartoon depictions of the president: Reagan as a cowboy with a bullet hole through the head, as a rodeo rider astride a missile and as a reptilian space invader from the television series "V" were among the most popular.

The protesters displayed buttons and T-shirts mixing support for Nicaragua's Sandinista government with demands that the United States withdraw its bases from Spain and that Spain pull out of the North Atlantic Treaty Organization.

The demonstrations coincided with the publication today of an opinion poll by the Madrid newspaper El Pais, the country's top-selling and most influential daily, that showed a majority of Spaniards opposing the president's policies.

The poll showed that of 1,290 questioned, 74 percent disagreed with the statement that Reagan's arms policy favored peace and reduced the danger of world war; that 66 percent agreed that the United States should reduce its military presence in Spain if Spain remains in NATO; that 65 percent disagreed that Reagan's economic policy favored less developed nations such as Spain and that only 15 percent agreed that the United States and its president were sincere and loyal friends of Spain.

The findings were mirrored by a poll conducted by the leading Spanish news magazine Cambio 16, showing that 47 percent disapproved of Reagan as the political leader of the West against 22 percent who approved.

The president is unlikely to experience such sentiments during his stay in Madrid because, aside from extraordinarily tight security, he is not scheduled to make any public appearances here. Reagan's timetable of meetings with King Juan Carlos and Prime Minister Felipe Gonzalez includes just one speech to an invited audience Tuesday at a private cultural center.

The protests today highlighted Spanish sensitivity over U.S. policy in Central America. To a far greater extent than other Western Europeans, Spaniards are supportive of the Sandinista government and angered by the trade embargo against Nicaragua.

In the Madrid demonstration, as the first marchers arrived at a large central square dedicated to the discovery of America and dominated by a statue of Christopher Columbus, three protesters scaled the statue's 60-foot column and unfurled a Sandinista flag to prolonged cheers.

The protests also focused on the issue of a referendum to decide Spain's future alignment in NATO -- a plebiscite that Gonzalez says he will hold early next year although he personally favors continued membership in the Atlantic Alliance.