How can you modify Python code for effective graph plotting?

  • Thread starter Thread starter Martyn Arthur
  • Start date Start date
  • Tags Tags
    Code Python
Click For Summary
SUMMARY

This discussion focuses on modifying Python code for effective graph plotting using the Matplotlib library. The user, Martyn, seeks to understand how to adjust the position and size of a scatter plot representing an H-R diagram of star cluster M35. Key insights include the use of plt.ylim() to set y-axis limits and the suggestion to use plt.xlim() for x-axis adjustments. The conversation emphasizes the importance of reading documentation and experimenting with parameters to achieve desired plot aesthetics.

PREREQUISITES
  • Familiarity with Python programming
  • Understanding of Matplotlib for data visualization
  • Basic knowledge of scatter plots and data representation
  • Experience with Pandas for data manipulation
NEXT STEPS
  • Learn how to use plt.xlim() to set x-axis limits in Matplotlib
  • Explore the Matplotlib documentation for advanced plotting techniques
  • Investigate data preprocessing with Pandas to enhance plot clarity
  • Experiment with different color maps in Matplotlib using cmap
USEFUL FOR

This discussion is beneficial for students in astrophysics, data scientists, and anyone interested in learning how to visualize data effectively using Python and Matplotlib.

Martyn Arthur
Messages
128
Reaction score
25
TL;DR Summary: How to understand the code determining the plot

Hi; this is the code I have been given for a H-R diagram of M35; I understand the inversion concept.. The plot is below. I am trying to understand the code that creates its position and size and how to alter it to a more 'acceptable' format please.
Thanks
Martyn
Python:
# Enter your code for your CMD plot here.
import pandas as pd              # Pandas is a common package for handling data as tables known as DataFrames
import numpy as np               # Numpy has essential tools for manipulating lists of data
import matplotlib.pyplot as plt  # matplotlib is the tool for plotting data
#ClusterData = pd.read_csv("MeasurementsM38.xls", sep = r"\s+") # Open the data file
#Above # as file is already open
display(ClusterData)
display(ClusterData.head(5)) # display the top 5 lines to check the data has been imported correctly
print(ClusterData.columns) # Print the column names for the DataFrame
keep_list = ClusterData['B_V'] < 2.0 # Questions which rows have a value less than 2.0 in the column B-V
print(keep_list) # Print this list
print('Length of ClusterData dataframe = ', len(ClusterData)) # Print the length of the DataFrame
ClusterData_red_removed = ClusterData[keep_list] # Save a new DataFrame based on the Boolean operator list
print('Length of ClusterData_red_removed dataframe = ', len(ClusterData_red_removed)) # Print the length
plt.rcParams['figure.figsize'] = [12, 8] # Set figure.figsize to be 12 inches wide and 8 inches high.
plt.scatter(                            # pyplot.scatter() function
    ClusterData_red_removed["B_V"],       # Required input - x values
    ClusterData_red_removed["Vmag"],      # Required input - y values
    c = ClusterData_red_removed["B_V"],   # Setting colour based on B-V colour
    cmap = "coolwarm",                  # Setting the colour map to use
    s = 3)                              # Setting datapoint size
plt.ylim((13, 0)) # Set y axis limits, with the higher number first to invert the axis

plt.title('Colour Magnitude diagram of M35 ') # Set plot title
plt.xlabel('Colour index (B_V)') # Set x axis label
plt.ylabel('Apparent V magnitude') # Set y axis label
plt.show() # Show the plot
1731585112864.png
 
Last edited by a moderator:
Physics news on Phys.org
Martyn Arthur said:
I am trying to understand the code that creates its ... size
What, this bit?
Python:
plt.rcParams['figure.figsize'] = [12, 8] # Set figure.figsize to be 12 inches wide and 8 inches high.

Martyn Arthur said:
I am trying to understand the code that creates its position
What do you mean by 'position'? Are you referring to the way all the data is clustered on the right of the plot? If so then you should be able to work out how to fix that by looking at
Python:
plt.ylim((13, 0)) # Set y axis limits, with the higher number first to invert the axis

Martyn Arthur said:
how to alter it to a more 'acceptable' format please.
That depends what you mean by 'acceptable' - personally I'd go for `with plt.xkcd():` :wink:

Seriously though, you can tweak everything you want if you Read The Fine Documentation for pyplot and for matplotlib's rcParams
 
  • Like
Likes   Reactions: berkeman and jedishrfu
Your data is your data and plots correctly on the axes.

xlim() and ylim() are your friends, which makes your plot more centered for viewing while keeping the axes properly labeled.
 
  • Like
Likes   Reactions: PhDeezNutz
Thread moved from the technical forums to the schoolwork forums
Thanks;; its an astrophysics course; we aren't expected to learn Python or being taught it. We are just given the Python notebooks to use to plot the data from, on this occasion star clusters which is what has happened, its worked and we have the plot.

Below is a plot of the same cluster Its x axis is -2 and 2 and the outliers to the right are visible.
Could you advise me please the code to set the x scale of my plot to the same levels?
I can get on then with evaluating my plot v the published version (I was required to plot 'just' 130 stars.

Thanks
Martyn




1731602547454.png
 
Last edited by a moderator:
Look at line 22 in the program you posted. That is where the Y-axis (vertical axis) of the graph is defined. The labeling starts at 13 at the bottom and goes to 0 at the top.

22 plt.ylim((13, 0)) # Set y axis limits, with the higher number first to invert the axis

The spot to define the X-axis (horizontal) would logically be in the blank line 23.

I am not familiar with the Python language, but I suspect the format for the X axis would be like line 22 only with "ylim" replaced by "xlim". The numbers would be the horizontal data range of the graph; which you can adjust so the plot includes the range of your data. (Note that negative numbers are allowed!)

Hope this helps.

Cheers,
Tom

p.s. Fortunately the program is very well documented with the comments. If you ever do any programming remember to use many comments. Even if you wrote it, coming back to a program without comments after a week can be very disturbing.
 
Martyn Arthur said:
Thanks;; its an astrophysics course; we aren't expected to learn Python or being taught it.
Maybe not, but you clearly are expected to work out how to use matplotlib.

Furthermore, because this is a science course you are expected to have an interest in science which includes a natural curiosity for why things are the way they are. We do this by observation and experiment. You can observe that the data is spread fairly evenly along the y-axis which ranges from about 13 to 0. You can also observe that the code includes a line commented as follows:
Python:
plt.ylim((13, 0)) # Set y axis limits, with the higher number first to invert the axis
So can you think of an experiment to see if you can affect the spread along the x-axis?
 
pbuk said:
Maybe not, but you clearly are expected to work out how to use matplotlib.

And knowing how to process and present data using python is a very useful skill in both academia and industry.

Most, if not all, standard python packages, including matplotlib, have comprehensive online documentation which will turn up towards the top of a google search, in this case as the first result for "matplotlib x-axis limits". Knowing how to google things and determine which results are trustworthy and unlikely to infect your device with malware is also a very important skill; do not delegate this to your AI assistant.
 
This is all really helpful; thanks
Martyn
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 16 ·
Replies
16
Views
4K
Replies
4
Views
5K