0

I am trying to plot a graph, but the x axis does not automatically format how I want. When I change this formatting, the graph no longer appears. Here is my code.

#start=float(t_array_hr[0])
#stop=float(t_array_hr[-1])
#xticks=(np.linspace(start,stop,8))

plt.plot(t_array_hr,Mdot_hr)
plt.xlabel('Time (s)')
plt.ylabel('Mdot in Solar Masses/yr')
plt.title('Rate of Mass Loss vs Time')
plt.gca().invert_yaxis()
#plt.xticks(xticks,xticks)

When run as above, I get the following graph. enter image description here

When the commented lines are included, I get this graph (the formatting is not complete but I have reserved that for a separate question.) enter image description here How can I format the x axis without affecting the graph itself?

lauchter
  • 21
  • 4
  • Is it possible that `t_array_hr` is not sorted, such that `t_array_hr[0]` and `t_array_hr[-1]` is not the appropriate range? – mechanical_meat Feb 11 '21 at 22:35
  • 1
    @mechanical_meat when I print t_array_hr it appears to be in ascending order as I want. – lauchter Feb 11 '21 at 22:38
  • 1
    Why do you need to use float() in the commented lines? – fdireito Feb 11 '21 at 22:57
  • @fdireito i got a dtype error on the linspace line if I do not specify them as floats – lauchter Feb 11 '21 at 23:09
  • 1
    it's hard to answer this question without a running example. you have too many variables undefined right now – Paul H Feb 12 '21 at 02:18
  • Any chance you could provide a [small reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) so that we can recreate the problem you are facing? Like those shown [here](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples), in the [pandas docs](https://pandas.pydata.org/pandas-docs/stable/user_guide/visualization.html#basic-plotting-plot) or in [this answer](https://stackoverflow.com/a/65675124/14148248). – Patrick FitzGerald Feb 12 '21 at 10:44
  • @PatrickFitzGerald Unfortunately my code is creating a dataframe by reading out a file on my computer and I do not think I will be able to attach that here. Thank you for your help anyway. – lauchter Feb 12 '21 at 22:23
  • Okay, instead you could just share the first few lines of your data file by copy-pasting them in your question like I have done [in this answer](https://stackoverflow.com/a/66102371/14148248) under `df.head()`. Also, what code are you using to read the file and create the dataframe? I believe the problem lies in how the time variable is being processed from the moment you import the data. If you provide this additional information I should be able to share a solution with an explanation. – Patrick FitzGerald Feb 13 '21 at 08:11
  • @PatrickFitzGerald unfortunately upon trying to get the first lines of my dataset I have realized that it is having formatting issues, and thus not giving the arrays correctly. I think I was premature in asking a question before trying every option. Thank you for your help regardless. – lauchter Feb 13 '21 at 23:09

0 Answers0