How can you modify Python code for effective graph plotting?

  • #1
Martyn Arthur
118
20
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
  • #2
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 berkeman and jedishrfu
  • #3
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 PhDeezNutz
  • #4
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:
  • #5
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.
 
  • #6
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?
 
  • #7
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.
 

FAQ: How can you modify Python code for effective graph plotting?

1. What libraries are commonly used for graph plotting in Python?

The most commonly used libraries for graph plotting in Python are Matplotlib, Seaborn, and Plotly. Matplotlib is a versatile library that provides a wide range of plotting capabilities. Seaborn is built on top of Matplotlib and offers a high-level interface for drawing attractive statistical graphics. Plotly is known for its interactive plots and is particularly useful for web applications.

2. How can I customize the appearance of my plots in Matplotlib?

You can customize the appearance of your plots in Matplotlib by modifying various parameters such as line styles, colors, markers, and labels. You can use functions like `plt.title()`, `plt.xlabel()`, and `plt.ylabel()` to add titles and labels. Additionally, you can set the figure size with `plt.figure(figsize=(width, height))` and change styles using `plt.style.use('style_name')` to apply predefined styles.

3. What is the best way to handle large datasets when plotting?

When handling large datasets for plotting, consider downsampling your data or using aggregation techniques to reduce the number of points plotted. Libraries like Datashader can help visualize large datasets efficiently. Additionally, using interactive plotting libraries like Plotly can allow users to zoom and pan through data without overwhelming the visual representation.

4. How can I save my plots as images in Python?

You can save your plots as images in Python using the `savefig()` function from Matplotlib. After creating your plot, simply call `plt.savefig('filename.png', dpi=300)` to save it as a PNG file with a specified resolution. You can also save in other formats like PDF, SVG, or JPG by changing the file extension in the filename.

5. What are some common mistakes to avoid when plotting graphs in Python?

Common mistakes to avoid include not labeling axes or adding titles, which can make plots hard to interpret. Additionally, using inappropriate scales (like linear vs. logarithmic) can misrepresent data. Another mistake is overcrowding the plot with too much information, which can confuse the viewer. Lastly, ensuring that color choices are accessible to those with color blindness is crucial for effective communication.

Similar threads

Back
Top