How to add legend for ** data.set_index ** in Python? - python-3.x

I have plotted this graph but do not know how to add legend and how to change color/shape of the graph.
Does anyone know how to do?
Thank you
Here are the codes and graphs that I have achieved

In the .plot() method, you can pass label and color (as well as a host of other things) as arguments. Color immediately uses the color you specify, but for the label to show up you also need to call plt.legend().
Example:
df["Some Data"].plot(color="green", label="This is some data")
plt.legend()
plt.show()

Related

show dates in xticks only where value exist in plot chart and hide unnecessary interpolated xtick labels [duplicate]

Could someone please guide me on how should I make sure that all ticks (or maybe a better way to specify will be to say all elements in the list passed to plot function) are displayed on the x axis when using matplotlib to plot graphs?
plt.plot(xValues, meanWeekdayArrivalCounts, 'k-')
I want all the values in the list xValues to show up on the graph. By default, only, 0, 10, 20, 30, 40, 50 show up.
Simply add plt.xticks(xValues) to your code. Given the number of points in your graph, the labels might clutter.
You could display them as minor ticks if you set them on the axes object with ax.set_xticks(xValues, minor=True).
use this.
fig = plt.figure(figsize=(10,8))
plt.xticks(np.arange(min(x), max(x)+1, 1.0))
I wanted to show all the years in my graph so I did
plt.xticks(list(table_05.anio.unique()))

Seaborn: Plot scatterplot over lineplot in same plot

I am trying to create one plot with one scatter and multiple lineplots.
For the points to be seen well, I need the scatterplot in the front.
However changing the order inside my code doesn't solve this. The
lineplots always cover the scatterplot. Any ideas?
Please help.
Use the zorder parameter when scatterplotting, e.g.
sns.scatterplot(x, y, ax=ax, color='orange', zorder=7)
You may need to adjust the zorder value depending on the number of elements in your plot.

plt.axvline labels exceeding limit of plot problem

full - clearest image, shows the labels extending out the right of my plot
"zoomed" - highlighting why this is a problem as the plot becomes unreadable when selecting and plotting subset of the x-axis range
I'm working on some data where I have plotted a spectrum and have added vertical lines to specific positions. I have labelled these lines but my problem is that if I want to "zoom in" by decreasing my x axis range, the full list of labels for the vertical lines are still plotted resulting in an unreadable plot.
I iterate through a list of x positions and labels for my vertical lines and plot as follows:
for x_pos, label in zip(list_x_pos, list_label):
plt.axvline(x=x_pos)
plt.text(x_pos, y, str(label), rotation=90)
plt.xlim(2, 5)
So, because my "x values" go > 5 the resulting plot is a tiny figure with a row of the labels extending out from it.
The only solution I can think of is to slice my list_x_pos but this will crate other problems for me so ideally looking to find a way to just show the labels within the range of the plot.
Hope I've made sense!
Thanks,
Olie
You can use plt.text(..., clip_on=True) to force texts outside of the box to not be displayed.
Resize the axes first and then plot the vertical line.

Draw axis thru x=0 and y=0

I have a set of data that I'm plotting as a scatter graph which has both positive and negative values on both axis. When I plot this in Flot, the axis are draw at the bottom and the left by default. Is there a way to make it draw the axis through the center of the graph? #X=0 and Y=0?
In other words, instead of this:
I want something like this:
That isn't possible in the default flot. I'm sure it could be hacked in if you wanted to dig into the source, but flot by itself only supports left/right for the y-axis, and top/bottom for the x-axis.
In case anybody else comes across the same need, I created a plugin for Flot and put it here:
https://github.com/burlandm/Flot-Origin-Axis
It does what I need, but I won't make any promises that it'll fit your particular scenario. If I have time, I might try and update it to cover more scenarios.

MATLAB: why would an axis have its position property changed?

I'm working on a custom plot on top of which I add a second blank axis object to show a secondary scale on the right hand side of the graph (as well as the primary scale on the left hand side).
Works great (and had been, for years), until I tried it again recently in R2010a and for some reason the primary axis gets its Position property tweaked when the figure is resized or printed, so that it looks OK on the screen at first, but when I print it or resize the figure, the two axes don't line up vertically.
What would do this, and how can I set it up so the two axes maintain the same position?
aha: I think I found it:
http://www.mathworks.com/access/helpdesk/help/techdoc/creating_plots/f1-32495.html
Controlling Axes Size
When you create a graph, MATLAB automatically creates an axes to display the graph. The axes is sized to fit in the figure and automatically resizes as you resize the figure. However, MATLAB applies the automatic resize behavior only when the axes Units property is set to normalized (the default).
Note: MATLAB changes only the current axes' properties by default. If your plot has multiple axes, MATLAB will not automatically resize any secondary axes.
You can control the resize behavior of the axes using the following axes properties:
...
ActivePositionProperty — Specifies whether to use the OuterPosition or the Position property as the size to preserve when resizing the figure containing the axes.
You may find the linkprop function helpful

Resources