0

EDITED

What I am starting with : I have multiple .xyz files for profiles ranging from

1982_1_Final.xyz - 1982_54_Final.xyz and such from 1982-2012

with Elevation/depth and Distance data columns to be able to plot horizontal profiles.

The Aim : I want to compare let say profile 25 for all years from 1982 until 2012,

so 1982_25_Final.xyz, 1983_25_Final.xyz, 1984_25_Final.xyz..... 2012_25_Final.xyz

What I have so far :

import os
import pandas as pd
import matplotlib.pyplot as plt

path = 'C:/Clayton/lines/profiles_aufmod'
os.chdir(path)
print(os.getcwd())

# Initiate figure
NUM_COLORS = 30
linestyle  = ['solid', 'dashed', 'dashdot', 'dotted']
NUM_STYLES = len(linestyle)
cm = plt.get_cmap('gist_rainbow')
fig = plt.figure()
ax = fig.add_subplot(111)

for directory_2, subdir_2, file_2 in os.walk(path):

    for ind, filename_2 in enumerate(file_2, 1):
        if filename_2.endswith("25_Final.xyz"):

            os.chdir(directory_2)
            Profile_raw_2 = pd.read_csv(
            filename_2, delimiter=',', names=["lat", "lon", "prof", "elev","dist"])

            lines = ax.plot(Profile_raw_2.iloc[:, 4], Profile_raw_2.iloc[:, 3], label=filename_2[0:29])

            for i in range(NUM_COLORS):

                 lines[0].set_color(cm(i // NUM_STYLES * float(NUM_STYLES) / NUM_COLORS))
                 lines[0].set_linestyle(linestyle[i % NUM_STYLES])

    os.chdir(path)
plt.show()

Problems I am facing : The nesting of the for loop is somehow wrong and I get all plots in one color and one linestyle, I cannot figure out the right order for the for loops here to get the desired output. Output_image

What I need help with :

  1. Nesting the for loop in the right order
  2. Making sure the colors are different for each year.
  3. How to change the colormap before hand to possibly get a sequential type which is good to indicate a yearly changing data.
  4. Any recommendations for such a plot?

Thank you in advance and sorry if there are some obvious mistakes, I am fairly new to python.

CaptClyt10
  • 13
  • 3
  • The aim to create 25 unique identifiable colors is ambitious - especially if you take into consideration that red-green impairments are quite common. It would be better to create a [custom cycler](https://stackoverflow.com/a/66259659/8881141) with fewer unique colors and add line styles such as `linestyle=['-', '--', ':', '-.']`. You will find also various threads on SO on how to [create semi-unique colors for plotting](https://stackoverflow.com/q/8389636/8881141). – Mr. T Mar 03 '21 at 11:45
  • Hello, thank you so much for your time, I tried to implement a solution given through the above link but am stumbling around the for loop order. Sorry if it is something obvious but I cannot seem to figure it. If you could look at the edited code above. Thank you – CaptClyt10 Mar 03 '21 at 12:43
  • Don't do this manually. The cycler properties are very useful because you can add or multiply line/marker characteristics that matplotlib then will apply to your graph instead of the standard cycler. I linked an example above, here is the link to the [matplotlib tutorial](https://matplotlib.org/stable/tutorials/intermediate/color_cycle.html#cycling-through-multiple-properties). – Mr. T Mar 03 '21 at 12:48
  • Hi @Mr.T Thank you , the custom color cycler worked well, I was wondering if I can maybe just use shades of one color for example white-red color bar, instead of many colors. – CaptClyt10 Mar 03 '21 at 13:11
  • You can choose a [sequential colormap](https://matplotlib.org/stable/tutorials/colors/colormaps.html) like "Reds" - but be careful that all lines are still visible. – Mr. T Mar 04 '21 at 11:51

0 Answers0