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()
Related
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 have a script for plotting big data sets. I have a problem while setting the xticks in my plot. I have tried the following code:
plt.xticks(configDBcomplete.index),max(configDBcomplete.index[-1]),5),data,rotation=90, fontsize= 12
The problem is that I have more than 2000 data points for x and the ticks get overlapped. I want to have ticks at every 5th data point. I have tried using np.arange as:
plt.xticks(np.arange(min(configDBcomplete.index),max(configDBcomplete.index[-1]),5),data,rotation=90, fontsize= 12
but it plots the first 50 data points along the plot and not the corresponding ones. Any idea how to solve this?
Currently you are using the whole data for setting the x-ticklabels. The first argument to the xticks() function is the location of the ticks and the second argument is the tick labels.
You need to use indexing to get every 5th data point (corresponding to the ticks). You can access it using [::5]. So you need to pass data[::5] to your xticks() as
plt.xticks(np.arange(min(configDBcomplete.index),max(configDBcomplete.index[-1]),5),data[::5],rotation=90, fontsize= 12)
You can also use range() as
plt.xticks(range(min(configDBcomplete.index),max(configDBcomplete.index[-1]),5),data[::5],rotation=90, fontsize= 12)
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.
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()
I'm making a scatter plot from a Pandas DataFrame with 3 columns. The first two would be the x and y axis, and the third would be classicfication data that I want to visualize by points having different colors. My question is, how can I add the legend to this plot:
df= df.groupby(['Month', 'Price'])['Quantity'].sum().reset_index()
df.plot(kind='scatter', x='Month', y='Quantity', c=df.Price , s = 100, legend = True);
As you can see, I'd like to automatically color the dots based on their price, so adding labels manually is a bit of an inconvenience. Is there a way I could add something to this code, that would also show a legend to the Price values?
Also, this colors the scatter plot dots on a range from black to white. Can I add custom colors without giving up the easy usage of c=df.Price?
Thank you!