Hi guys sorry if bad question but I have this histogram (pic attached) I need to somehow in my code include 'tick marks' up the horizontal axis as well as numbers 0,2,4,,6,8,10, 10 being the maximum in my example.
I have no idea how to add these tick marks I cannot use imports, dicts, anything like that. The closest idea that I have is some loop including
i * ([(max-min)/5])
where max and min are the beginning and end of the horizontal axis.
Have been staring at this for over a week and this is the final piece and I am drawing a blank so any help would be very appreciated!
To customise the appearance of the ticks, you can use the Axes.tick_params() method.
ex:
fig, ax = plt.subplots()
ax.plot(datasetname['Bronze'],women_degrees['Craft'],label='datasetname')
ax.tick_params(bottom="on", top="off", left="on", right="off")
plt.show()
Related
I have a problem with long xticks text.
this my code.
fig = px.bar(dataa)
fig.update_layout(yaxis_title="Owners_Jurisdiction Count", xaxis_title="Owners")
fig.update_xaxes(tickangle=45, showgrid=True)
fig.show()
but what i got is the below picture:
enter image description here
I want all long xticks text are truncated to 7 characters(for example)
This question has been asked a few times before:
How to shorten and show only a part of text in plotly express axis?
Is it possible to limit or truncate the characters of the tick label in plotly?
Answer from first post:
To shorten the tick labels of your x-axis, you could either change the id's of your column beforehand by changing the dataframe, or you could use .update_layout() to change your tick labels afterwards.
If the question is how to shorten the x-axis string to 7 characters because it is long, the x-axis ticks can be updated after drawing, so the list is set up using comprehension notation.
fig = px.bar(dataa)
fig.update_layout(yaxis_title="Owners_Jurisdiction Count", xaxis_title="Owners")
fig.update_traces(x=[x[:7] for x in df['Owners_name']])
fig.update_xaxes(tickangle=45, showgrid=True)
fig.show()
I want to plot multiple lines in the same plot, like in the picture below:
The problem with the picture is that if the Y values of the graphs aren't similar the y ticks get jumbled, it's unclear which tick is related to the first graph and which one isn't.
Is there a way for me to colour the ticks of each graph differently (preferably to the colour of the graph)? or maybe separate it into different columns?
Also, I wouldn't mind using more than one subplot, as long as the graphs' space overlaps.
The code I use to create the new lines:
def generate_graph():
colors = "rgbmcmyk"
subplot_recent.clear()
lines_drawn = []
mat_figure.legends = []
for i in range(n):
lines_drawn.append(["A Name", subplot_recent.plot(values[i][0], values[i][1], colors[i])[0]])
mat_figure.legend((i[1] for i in lines_drawn), (i[0] for i in lines_drawn), 'upper right')
subplot_recent.yaxis.set_major_locator(plt.MaxNLocator(10))
mat_canvas.draw()
The error actually was that I forgot to cast the values to int/float, and so matplotlib didn't really know what to do with them all to well.
It's fixed now. Thanks!
I am relatively new to coding, so I apologize beforehand for this simple question:
I want to plot 2week candlesticks.
After I resampled my dataset in 2 week chunks I plotted the results. Unfortunately, matplotlib plots the chart with the complete date range, meaning that there are 14 day gaps between each candle. I already have tried to use ax.xaxis.set_major_locator(WeekdayLocator(byweekday=MO, interval=2)) but this just formats the labels of the x-axis, not the used values.
The Code:
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.xaxis.set_major_formatter(weekFormatter)
candlestick_ohlc(ax, zip(mdates.date2num(quotes.index.to_pydatetime()),
quotes['open'], quotes['high'],
quotes['low'], quotes['close']),
width=0.6)
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()
Heres the result:
So how can I create a continuous graph where the candlesticks are closer to each other ?
[EDIT]
Wow, the simple solution is to put the width higher... I am really sorry for this. It was my first post here :D
I think the problem is with minor locator, which makes smaller marks on x axis every day. You can use ax.xaxis.minorticks_off() to disable them.
[EDIT]
Hmm, now that I reread the question I think that you want candlesticks to be wider. There is width parameter to do just so.
I am trying to plot a graph using Matplotlib using the following code:
fig, axs = plt.subplots()
axs.set_xlim([1,5])
axs.grid()
axs.errorbar(plot1_dataerr[1],range(len(plot1_dataerr[1])),xerr = plot1_dataerr[2], fmt = 'k o')
axs.yaxis.set_ticks(np.arange(len(plot1_dataerr[1])))
axs.set_yticklabels(plot1_dataerr[0])
The variable plot1_dataerr contains the labels for the data as its 0th element, the actual means as the 1st element and the half-length of the error bars as the second element. When I run this code (along with the exact data) I get the following:
However as you can see some of the ticks on the y-axis are cut off, they should all start with 'vegetable based side dishes'. Does anyone know what I should change so that everything fits. I don't mind if some of the labels need to occupy 2 lines.
Thanks in advance!
You probably need to increase the left margin. For automatic adjustment, use
fig.tight_layout()
Else, start with
fig.subplots_adjust(left=0.4)
and decrease the value until you are happy with the result.
I have dates for tick marks on my x-axis. How can I make them automatically not overlap?
There are a lot of S.O. questions and posts about setting the tick interval - but this won't work for me since the date range for my plot can vary from 1 week, up to 1 year.
When people have plots with highly variable ranges, what is the method to make the x-axis ticks automatically not overlap?
plt.plot(date_list, unique_list)
plt.ylabel('# Uniques per day')
You could rotate the xticks by 90 degrees (or any other value):
plt.xticks(rotation=90)
maybe you need to call tight_layout() if the ticks are then out of the frame
plt.tight_layout()
Don't have enough rep to comment, because I don't think the tip I am offering could solve this problem, but I had exactly the same issue.
My solution is to make the plot itself flatter, by
plt.figure(figsize = (20,6)).
My reasoning is as such, there are only so many pixels and if changing the date format and rotation as suggested by others is not an option, maybe stretch the plot a bit?